1. 接口说明
积分查询接口用于在调用正式识别接口前,获知单次请求所消耗的积分数。URL 中 您要查询的api名称 须与各 API 文档中的路径段一致(与正式接口同源,仅末尾路径为 /v1/credit)。
1.1 路径示例
- 通用(高精)OCR:将路径中的标识换为
advanced_general_ocr - 其他接口:请对照对应文档中的
/api/<标识>/v1,将同一标识填入本接口 URL。
2. 请求信息
2.1 请求地址(URL)
POST https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit
将 您要查询的api名称 替换为具体 API 路径段,例如 advanced_general_ocr。
2.2 请求方式
POST
2.3 请求头(header)
| 参数 | 类型 | 说明 |
|---|---|---|
| Content-Type | string | application/json |
| Authorization | string | APPCODE + 您的 AppCode(注意 APPCODE 后为一个英文空格)获取 |
| 参数 | 类型 | 说明 |
|---|---|---|
| Content-Type | string | application/json |
| x-ca-key | string | 您的 AppKey获取 |
| x-ca-timestamp | string | 时间戳(毫秒) |
| x-ca-signature | string | 签名 sign |
str = app_key×tamp&app_secret sign = md5(str)
2.4 请求体(body)
无请求体,无需传递 JSON 字段。
3. 返回信息
3.1 返回类型
JSON
3.2 返回字段
| 参数名 | 类型 | 说明 |
|---|---|---|
| code | int | 状态码,200 表示成功 |
| msg | string | 说明(英文) |
| msg_cn | string | 说明(中文) |
| success | bool | 是否成功 |
| file_id | string | 关联标识(若返回) |
| request_id | string | 请求 ID |
| credit | number | 单次调用所需积分数 |
3.3 返回示例
成功示例:
{
"code": 200,
"msg": "OK",
"msg_cn": "成功",
"success": true,
"file_id": "",
"request_id": "请求唯一ID",
"credit": 10
}
3.4 错误码
| 错误码 | 说明 |
|---|---|
| 200 | 成功 |
| 400 | 请求错误 |
| 401 | 未授权 |
| 500 | 服务错误 |
具体失败原因请结合返回中的 msg、msg_cn 排查。
4.示例代码
4.1 Python
# -*- coding: utf-8 -*-
import requests
import json
# 将 api_slug 换成与正式接口一致的路径段,例如 advanced_general_ocr
API_SLUG = "您要查询的api名称"
URL = "https://ocr-api.shiliuai.com/api/%s/v1/credit" % API_SLUG
def demo(appcode):
headers = {
"Authorization": "APPCODE %s" % appcode,
"Content-Type": "application/json",
}
# 无请求体
response = requests.post(url=URL, headers=headers)
content = json.loads(response.content)
print(content)
if __name__ == "__main__":
appcode = "你的APPCODE"
demo(appcode)
# -*- coding: utf-8 -*-
import requests
import json
import hashlib
import time
API_SLUG = "您要查询的api名称"
URL = "https://ocr-api.shiliuai.com/api/%s/v1/credit" % API_SLUG
def md5(s):
return hashlib.md5(s.encode("utf8")).hexdigest()
def demo(app_key, app_secret):
t = int(time.time() * 1000)
s = "%s&%s&%s" % (app_key, t, app_secret)
sign = md5(s)
headers = {
"x-ca-key": app_key,
"x-ca-timestamp": t,
"x-ca-signature": sign,
"Content-Type": "application/json",
}
response = requests.post(url=URL, headers=headers)
content = json.loads(response.content)
print(content)
if __name__ == "__main__":
app_key = "你的APP_KEY"
app_secret = "你的APP_SECRET"
demo(app_key, app_secret)
4.2 PHP
$url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"; $appcode = "你的appcode"; $method = "POST"; // 请求头 $headers = array(); array_push($headers, "Authorization:APPCODE " . $appcode); array_push($headers, "Content-Type:application/json"); // 无请求体 $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POSTFIELDS, ""); $result = curl_exec($curl); var_dump($result);
$url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"; $method = "POST"; $app_key = "你的app_key"; $app_secret = "你的app_secret"; $timestamp = intval(round(microtime(true) * 1000)); $sign_string = $app_key . "&" . $timestamp . "&" . $app_secret; $sign = md5($sign_string); $headers = array(); array_push($headers, "Content-Type:application/json"); array_push($headers, "x-ca-key:" . $app_key); array_push($headers, "x-ca-timestamp:" . $timestamp); array_push($headers, "x-ca-signature:" . $sign); $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POSTFIELDS, ""); $result = curl_exec($curl); var_dump($result);
4.3 Java
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit";
String appcode = "你的APPCODE";
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "APPCODE " + appcode);
headers.put("Content-Type", "application/json");
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
httpPost.setEntity(new StringEntity("", ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(httpPost);
int stat = response.getStatusLine().getStatusCode();
if (stat != 200) {
System.out.println("Http code: " + stat);
return;
}
String res = EntityUtils.toString(response.getEntity());
JSONObject res_obj = JSON.parseObject(res);
System.out.println(res_obj.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.util.HashMap;
import java.util.Map;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Demo {
public static String MD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit";
String app_key = "你的APPKEY";
String app_secret = "你的APPSECRET";
String timestamp = System.currentTimeMillis() + "";
String sign = MD5(app_key + "&" + timestamp + "&" + app_secret);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("x-ca-key", app_key);
headers.put("x-ca-timestamp", timestamp);
headers.put("x-ca-signature", sign);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
httpPost.setEntity(new StringEntity("", ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(httpPost);
int stat = response.getStatusLine().getStatusCode();
if (stat != 200) {
System.out.println("Http code: " + stat);
System.out.println("Http " + EntityUtils.toString(response.getEntity()));
return;
}
String res = EntityUtils.toString(response.getEntity());
JSONObject res_obj = JSON.parseObject(res);
System.out.println(res_obj.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.4 JavaScript
// 需 Node.js 18+(内置 fetch)
const apiUrl = 'https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit';
const appcode = '你的APPCODE';
async function main() {
const res = await fetch(apiUrl, {
method: 'POST',
headers: {
Authorization: 'APPCODE ' + appcode,
'Content-Type': 'application/json'
}
});
const text = await res.text();
if (!res.ok) {
console.error('Http code:', res.status, text);
return;
}
console.log(JSON.stringify(JSON.parse(text), null, 2));
}
main().catch(console.error);
// 需 Node.js 18+(内置 fetch)
const crypto = require('crypto');
const apiUrl = 'https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit';
const appKey = '你的APPKEY';
const appSecret = '你的APPSECRET';
const timestamp = Date.now().toString();
const signHex = crypto.createHash('md5').update(appKey + '&' + timestamp + '&' + appSecret).digest('hex');
async function main() {
const res = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-ca-key': appKey,
'x-ca-timestamp': timestamp,
'x-ca-signature': signHex
}
});
const text = await res.text();
if (!res.ok) {
console.error('Http code:', res.status, text);
return;
}
console.log(JSON.stringify(JSON.parse(text), null, 2));
}
main().catch(console.error);
4.5 C#
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyCSharpApp
{
public class Program
{
public static async Task Main(string[] args)
{
string url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit";
string appcode = "你的APPCODE";
Dictionary<string, string> headers = new Dictionary<string, string>
{
{ "Authorization", "APPCODE " + appcode }
};
string body = "";
try
{
using (HttpClient client = new HttpClient())
{
foreach (var header in headers)
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
StringContent content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Http code: {(int)response.StatusCode}");
return;
}
string responseContent = await response.Content.ReadAsStringAsync();
JObject resObj = JObject.Parse(responseContent);
Console.WriteLine(resObj.ToString(Formatting.Indented));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MyCSharpApp
{
public class Program
{
public static string CalculateMD5(string input)
{
try
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return string.Empty;
}
}
public static async Task Main(string[] args)
{
string url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit";
string app_key = "你的APPKEY";
string app_secret = "你的APPSECRET";
string timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
string sign = CalculateMD5(app_key + "&" + timestamp + "&" + app_secret);
Dictionary<string, string> headers = new Dictionary<string, string>
{
{ "x-ca-key", app_key },
{ "x-ca-timestamp", timestamp },
{ "x-ca-signature", sign }
};
string body = "";
try
{
using (HttpClient client = new HttpClient())
{
foreach (var header in headers)
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
StringContent content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Http code: {(int)response.StatusCode}");
Console.WriteLine($"Http {await response.Content.ReadAsStringAsync()}");
return;
}
string responseContent = await response.Content.ReadAsStringAsync();
JObject resObj = JObject.Parse(responseContent);
Console.WriteLine(resObj.ToString(Formatting.Indented));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
4.6 易语言
版本 2 .支持库 spec .支持库 dp1 .子程序 积分查询_简单认证 .局部变量 局_网址, 文本型 .局部变量 局_提交数据, 文本型 .局部变量 局_提交协议头, 文本型 .局部变量 局_结果, 字节集 .局部变量 局_返回, 文本型 局_提交数据 = "" ' 无请求体 局_网址 = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit" 局_提交协议头 = "Authorization: APPCODE 你的AppCode" + #换行符 + "Content-Type: application/json" 局_结果 = 网页_访问_对象 (局_网址, 1, 局_提交数据, , , 局_提交协议头, , , , , , , , , , , , , ) 局_返回 = 到文本 (编码_编码转换对象 (局_结果, , , )) 返回 (局_返回)
版本 2 .支持库 spec .支持库 dp1 .子程序 积分查询_签名认证 .参数 app_key, 文本型 .参数 app_secret, 文本型 .局部变量 局_网址, 文本型 .局部变量 局_提交数据, 文本型 .局部变量 局_提交协议头, 文本型 .局部变量 局_结果, 字节集 .局部变量 局_返回, 文本型 .局部变量 时间戳, 文本型 .局部变量 签名字符串, 文本型 .局部变量 签名, 文本型 局_提交数据 = "" ' 无请求体 时间戳 = 到文本 (时间_取时间戳 () * 1000) 签名字符串 = app_key + "&" + 时间戳 + "&" + app_secret 签名 = 到十六进制文本 (编码_MD5摘要 (到字节集 (签名字符串))) 局_网址 = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit" 局_提交协议头 = "x-ca-key: " + app_key + #换行符 + "x-ca-timestamp: " + 时间戳 + #换行符 + "x-ca-signature: " + 签名 + #换行符 + "Content-Type: application/json" 局_结果 = 网页_访问_对象 (局_网址, 1, 局_提交数据, , , 局_提交协议头, , , , , , , , , , , , , ) 局_返回 = 到文本 (编码_编码转换对象 (局_结果, , , )) 返回 (局_返回)
4.7 天诺
public static string CreditQuery_Easy(string appcode)
{
string url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit";
var headers = new Dictionary<string, string>
{
{"Authorization", "APPCODE " + appcode},
{"Content-Type", "application/json"}
};
return CustomHelp.HttpPost(url, "", headers);
}
public static string app_key = "你的APPKEY";
public static string app_secret = "你的APPSECRET";
public static string GetTimestampMs()
{
return DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
}
public static string MD5(string input)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
var bytes = System.Text.Encoding.UTF8.GetBytes(input);
var hash = md5.ComputeHash(bytes);
var sb = new System.Text.StringBuilder();
foreach (var b in hash) sb.Append(b.ToString("x2"));
return sb.ToString();
}
}
public static string CreditQuery_Sign()
{
string url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit";
string timestamp = GetTimestampMs();
string sign = MD5(app_key + "&" + timestamp + "&" + app_secret);
var headers = new Dictionary<string, string>
{
{"x-ca-key", app_key},
{"x-ca-timestamp", timestamp},
{"x-ca-signature", sign},
{"Content-Type", "application/json"}
};
return CustomHelp.HttpPost(url, "", headers);
}
4.8 按键精灵-电脑版
VBSBegin
Function credit_easy(appcode)
url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "POST", url, False
http.setRequestHeader "Authorization", "APPCODE " & appcode
http.setRequestHeader "Content-Type", "application/json"
http.send ""
credit_easy = http.responseText
End Function
VBSEnd
appcode = "你的APPCODE"
res = credit_easy(appcode)
TracePrint res
Import "Encrypt.dll"
VBSBegin
Function MD5(str)
MD5 = LCase(Encrypt.Md5String(str))
End Function
Function credit_sign(appKey, appSecret)
url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
timestamp = CStr(DateDiff("s", "01/01/1970 00:00:00", Now()) * 1000)
signature = MD5(appKey & "&" & timestamp & "&" & appSecret)
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "POST", url, False
http.setRequestHeader "x-ca-key", appKey
http.setRequestHeader "x-ca-timestamp", timestamp
http.setRequestHeader "x-ca-signature", signature
http.setRequestHeader "Content-Type", "application/json"
http.send ""
credit_sign = http.responseText
End Function
VBSEnd
appKey = "你的APPKEY"
appSecret = "你的APPSECRET"
res = credit_sign(appKey, appSecret)
TracePrint res
4.9 按键精灵-手机版
Import "yd.luae"
Function credit_easy(appcode)
Dim url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
Dim body = ""
Dim headers = {null}
headers["Authorization"] = "APPCODE " & appcode
headers["Content-Type"] = "application/json"
Dim res = yd.HttpPost(url, body, headers)
credit_easy = yd.JsonDecode(res)
End Function
Dim appcode = "你的 APPCODE"
Dim res = credit_easy(appcode)
TracePrint res["code"]
Import "yd.luae"
Dim appKey = "你的 APPKEY"
Dim appSecret = "你的 APPSECRET"
Function credit_sign(appKey, appSecret)
Dim url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
Dim timestamp = CStr(os.time()*1000)
Dim signature = Encode.Md5(appKey & "&" & timestamp & "&" & appSecret)
Dim body = ""
Dim headers = {null}
headers["x-ca-key"] = appKey
headers["x-ca-timestamp"] = timestamp
headers["x-ca-signature"] = signature
headers["Content-Type"] = "application/json"
Dim res = yd.HttpPost(url, body, headers)
credit_sign = yd.JsonDecode(res)
End Function
Dim res = credit_sign(appKey, appSecret)
TracePrint res["code"]
4.10 触动精灵
require("tsnet")
local ts = require("ts")
local json = ts.json
function credit_easy(appcode)
local url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
local headers = {}
headers["Authorization"] = "APPCODE " .. appcode
headers["Content-Type"] = "application/json"
local resp = httpPost(url, "", { headers = headers })
return json.decode(resp)
end
require("tsnet")
local ts = require("ts")
local json = ts.json
function credit_sign(appKey, appSecret)
local url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
local timestamp = tostring(os.time()*1000)
local signature = (appKey.."&"..timestamp.."&"..appSecret):md5()
local headers = {}
headers["x-ca-key"] = appKey
headers["x-ca-timestamp"] = timestamp
headers["x-ca-signature"] = signature
headers["Content-Type"] = "application/json"
local resp = httpPost(url, "", { headers = headers })
return json.decode(resp)
end
4.11 懒人精灵
function credit_easy(appcode)
local url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
local headers = {}
headers["Authorization"] = "APPCODE " .. appcode
headers["Content-Type"] = "application/json"
local resp = httpPost(url, "", { headers = headers })
return jsonLib.decode(resp)
end
function credit_sign(appKey, appSecret)
local url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
local timestamp = tostring(os.time()*1000)
local signature = MD5(appKey.."&"..timestamp.."&"..appSecret)
local headers = {}
headers["x-ca-key"] = appKey
headers["x-ca-timestamp"] = timestamp
headers["x-ca-signature"] = signature
headers["Content-Type"] = "application/json"
local resp = httpPost(url, "", { headers = headers })
return jsonLib.decode(resp)
end
4.12 EasyClick
function main()
local appCode = "你的 APPCODE"
local res = credit_easy(appCode)
logd(res.code)
end
function credit_easy(appCode)
local url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
local params = {
url = url,
method = "POST",
headers = {
["Authorization"] = "APPCODE " .. appCode,
["Content-Type"] = "application/json"
},
requestBody = ""
}
local res = http.request(params)
return JSON.parse(res.body)
end
function main()
local appKey = "你的 APPKEY"
local appSecret = "你的 APPSECRET"
local res = credit_sign(appKey, appSecret)
logd(res.code)
end
function credit_sign(appKey, appSecret)
local url = "https://ocr-api.shiliuai.com/api/您要查询的api名称/v1/credit"
local timestamp = tostring(os.time() * 1000)
local signature = utils.dataMd5(appKey .. "&" .. timestamp .. "&" .. appSecret)
local params = {
url = url,
method = "POST",
headers = {
["x-ca-key"] = appKey,
["x-ca-timestamp"] = timestamp,
["x-ca-signature"] = signature,
["Content-Type"] = "application/json"
},
requestBody = ""
}
local res = http.request(params)
return JSON.parse(res.body)
end