调用所有API2.0的接口需要headers中提交:
- 开发者识别码 (headers【DeveloperKey】 - 联系技术支持提供基本公式信息可获取)
- 令牌(headers【Authorization】 - 请查看以下的获取令牌方式)
验证使用OAuth2.0协议JWT(JSON Web Token)认证模型
获取令牌方式:
提交 POST 请求 (x-www-form-urlencoded) 到 URL:
测试环境 | http://api.edu.cdek.ru/v2/oauth/token?parameters |
---|---|
正式环境 | http://api.cdek.ru/v2/oauth/token?parameters |
中国正式环境 | https://int.cdek-express.cn/v2/oauth/token?parameters |
参数描述以及参数值:
grant_type: 验证类型, 可用的参数值: client_credentials;
client_id: 客户识别码, 等于 account 值;
client_secret: 客户密码, 等于 secure_password 值。
返回数据内容:
access_token: jwt-令牌;
token_type: 令牌类型 (一直等于 “bearer”);
expires_in: 令牌的有效时间,单位:秒 (默认 3600 秒);
scope: 使用范围 (权限);
jti: 令牌唯一识别码.
调用要求授权的接口时需要把’Bearer ‘字段跟着令牌放到headers Authorization参数中,比如:
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI…
注意Bearer 字段中B字母是大写的
获取令牌提交的数据以及返回的数据
获取令牌请求参数内容例子:
grant_type | client_credentials |
---|---|
client_id | z9GRRu7FxmO53CQ9cFfI6qiy32wpfTkd |
client_secret | w24JTCv4MnAcuRTx0oHjHLDtyt3I6IBq |
**
Python 3 获取令牌的例子
# encoding: utf-8
import requests
import urllib.parse
account = '1234569e96ece5b0545d472babcdf' #需要使用您的合同对应的对接账号
secure_password = 'adfc3673764e27c8f822ab285d1234567' #需要使用您的合同对应的对接密码
auth_url = 'http://api.cdek.ru/v2/oauth/token' #正式环境的URL
params = {'grant_type':'client_credentials', 'client_id':account, 'client_secret': secure_password}
full_auth_url = auth_url+'?'+urllib.parse.urlencode(params) #生成完整的URL
headers = {'Content-type': 'x-www-form-urlencoded'}
print(f'Request to:\n{full_auth_url}')
#Request to:
#http://api.cdek.ru/v2/oauth/token?grant_type=client_credentials&client_id=12345...&client_secret=b0a...
responce = requests.post(url=full_auth_url, headers=headers)
responce_data = responce.json()
if 'error' in responce_data:
print(f'Error: {responce_data["error"]}')
else:
access_token = responce_data["access_token"]
print(f'Responce data:\n{responce_data}')
print(f'access_token: {access_token}')
# 加下来所有要求认证的请求中headers中要加上获取的'Bearer '字段加令牌:
headers = {'Authorization': 'Bearer '+access_token, 'Content-type': 'application/json'}
获取令牌成功的返回数据例子
{
"access_token": "5p52g......",
"token_type": "bearer",
"expires_in": 3599,
"scope": "order:all",
"jti": "2b8bacf8-......"
}
错误处理
获取令牌请求返回数据中有 “error” 符号的话意味着获取令牌失败
否则请求成功并且返回数据中必须存在 “access_token”- 令牌值
错误例子
account 或 secure_password 错误的或者账号被封闭时候请求返回的数据:
{
'error': 'invalid_client',
'error_description': 'Bad client credentials'
}
处理方法:
- 确认account和secure_password值是正确的
- 咨询CDEK客户经理该客户是否被封闭(由于欠费、合同解约或者其他原因)
获取令牌请求URL没加上 grant_type、client_id、client_secret 参数时候返回的内容:
{
'timestamp': 1584500307779,
'status': 401,
'error': 'Unauthorized',
'message': 'Full authentication is required to access this resource',
'path': '/oauth/token'
}
处理方法:确认请求url包含grant_type、client_id、client_secret的参数,正确的URL例子:http://api.cdek.ru/v2/oauth/token?grant_type=client_credentials&client_id=12345...&client_secret=adfc3…
调用其他需要授权的接口时候如果headers Authorization中bearer字段把b字母小写的话、请求返回:
{
"timestamp": 1584501484081,
"path": "/v2/webhooks",
"status": 500,
"error": "Internal Server Error",
"message": "Значение JWT токена должно быть задано след. шаблоном: [Bearer] + [пробел] + [JWT токен]"
}
message:提交的JWT值必须符合以下模板:[Bearer] + [空格] + [JWT令牌]