鉴权认证机制
简介
鉴权的主要目的是获取Access_token。Access_token是用户的访问令牌,承载了用户的身份、权限等信息。鉴权主要分为以下两步:
1.获取AK/SK
2.获取Access_token
- 代码形式→适用于有计算机基础的用户
- 网页调试工具→适用于零基础的用户
获取AK/SK
- API 为收费调用
- 优先为公司提供服务支持
- 价格:0.4元/次【大量调用请商务对接】
- 联系我们
获取Access_token
晟邦API开放平台使用OAuth2.0授权调用开放API,调用API时必须在URL中带上Access_token参数,Access token默认有效期为 30 天,获取Access_token的流程如下:
请求URL数据格式
URL
https://openapi.ocr.sys303.com/api/v1/oauth/token?client_id={API Key}&client_secret={Secret Key}&grant_type=client_credentials
- grant_type: 必须参数,固定为client_credentials;
- client_id: 必须参数,应用的API Key;
- client_secret: 必须参数,应用的Secret Key;
POST 方式请求
例如:
https://openapi.ocr.sys303.com/api/v1/oauth/token?client_id=Fq5eR3LT0vuXV&client_secret=0rDSjzQ20XUj5itV6WRtz&grant_type=client_credentials
请求样例
- bash
- python
- C#
- Java
curl -X 'POST' \
'https://openapi.ocr.sys303.com/api/v1/oauth/token?client_id=【替换为有效client_id】&client_secret=【替换为有效client_secret】&grant_type=client_credentials' \
-H 'accept: application/json' \
-d ''
import requests
import json
def main():
client_id = '【替换为有效client_id】'
client_secret = '【替换为有效client_secret】'
url = f"https://openapi.ocr.sys303.com/api/v1/oauth/token?client_id={client_id}&client_secret={client_secret}&grant_type=client_credentials"
payload = ""
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
if __name__ == '__main__':
main()
using System;
using System.Collections.Generic;
using System.Net.Http;
public static class AccessToken
{
// 调用getAccessToken()获取的 access_token建议根据 expires_in 时间 设置缓存
// 返回token示例
public static String TOKEN = "qwertyuiop.zxcvbnm.asdfghjkl-1234567";
// API Key
private static String clientId = "【替 换为有效clientId】";
// Secret Key
private static String clientSecret = "【替换为有效clientSecret】";
public static String getAccessToken() {
String authHost = "https://openapi.ocr.sys303.com/api/v1/oauth/token?grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret;
HttpClient client = new HttpClient();
List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
return result;
}
public static void Main(string[] args)
{
getAccessToken();
}
}
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class AuthService {
/**
* 获取权限token
* @return 返回示例:
* {
* "access_token": "qwertyuiop.zxcvbnm.asdfghjkl-1234567",
* "expires_in": 1752371364
* }
*/
public static String getAuth() {
String clientId = "【替换为有效clientId】";
String clientSecret = "【替换为有效clientSecret】";
return getAuth(clientId, clientSecret);
}
/**
* 获取API访问token
* 该token有一定的有效期,需要自行管理,当失效时需重新获取.
* @param ak - API Key
* @param sk - Securet Key
* @return assess_token 示例:
* "qwertyuiop.zxcvbnm.asdfghjkl-1234567"
*/
public static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = "https://openapi.ocr.sys303.com/api/v1/oauth/token?";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. API Key
+ "&client_id=" + ak
// 3. Secret Key
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.getOutputStream().write("".getBytes());
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 返回结果示例
*/
System.err.println("result:" + result);
JSONObject jsonObject = new JSONObject(result);
String access_token = jsonObject.getString("access_token");
return "access_token";
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
}
返回格式
JSON格式
成功报文
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xxxxxE1MTQ5NDcsImlzcyI6Imh0dHBzOi8vYXBpLm9jci5zeXxxxxxxxaHR0cHM6Ly9vY3Iuc3lzMzAzLmNvbSIsIm5hbWUxxxxxx.xxxxxx8csbHvSKSXtjDcc0zxxxxxxx",
"expires": 1731514947
}
异常报文
{
"error_code": 6,
"error_msg": "No permission to access data",
"details": null
}