调用所有API2.0的接口需要headers中提交:

  1. 开发者识别码 (headers【DeveloperKey】 - 联系技术支持提供基本公式信息可获取)
  2. 令牌(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 获取令牌的例子

  1. # encoding: utf-8
  2. import requests
  3. import urllib.parse
  4. account = '1234569e96ece5b0545d472babcdf' #需要使用您的合同对应的对接账号
  5. secure_password = 'adfc3673764e27c8f822ab285d1234567' #需要使用您的合同对应的对接密码
  6. auth_url = 'http://api.cdek.ru/v2/oauth/token' #正式环境的URL
  7. params = {'grant_type':'client_credentials', 'client_id':account, 'client_secret': secure_password}
  8. full_auth_url = auth_url+'?'+urllib.parse.urlencode(params) #生成完整的URL
  9. headers = {'Content-type': 'x-www-form-urlencoded'}
  10. print(f'Request to:\n{full_auth_url}')
  11. #Request to:
  12. #http://api.cdek.ru/v2/oauth/token?grant_type=client_credentials&client_id=12345...&client_secret=b0a...
  13. responce = requests.post(url=full_auth_url, headers=headers)
  14. responce_data = responce.json()
  15. if 'error' in responce_data:
  16. print(f'Error: {responce_data["error"]}')
  17. else:
  18. access_token = responce_data["access_token"]
  19. print(f'Responce data:\n{responce_data}')
  20. print(f'access_token: {access_token}')
  21. # 加下来所有要求认证的请求中headers中要加上获取的'Bearer '字段加令牌:
  22. headers = {'Authorization': 'Bearer '+access_token, 'Content-type': 'application/json'}

获取令牌成功的返回数据例子

  1. {
  2. "access_token": "5p52g......",
  3. "token_type": "bearer",
  4. "expires_in": 3599,
  5. "scope": "order:all",
  6. "jti": "2b8bacf8-......"
  7. }

错误处理

获取令牌请求返回数据中有 “error” 符号的话意味着获取令牌失败
否则请求成功并且返回数据中必须存在 “access_token”- 令牌值

错误例子

account 或 secure_password 错误的或者账号被封闭时候请求返回的数据:

  1. {
  2. 'error': 'invalid_client',
  3. 'error_description': 'Bad client credentials'
  4. }

处理方法:

  1. 确认account和secure_password值是正确的
  2. 咨询CDEK客户经理该客户是否被封闭(由于欠费、合同解约或者其他原因)

获取令牌请求URL没加上 grant_typeclient_idclient_secret 参数时候返回的内容:

  1. {
  2. 'timestamp': 1584500307779,
  3. 'status': 401,
  4. 'error': 'Unauthorized',
  5. 'message': 'Full authentication is required to access this resource',
  6. 'path': '/oauth/token'
  7. }

处理方法:确认请求url包含grant_typeclient_idclient_secret的参数,正确的URL例子:http://api.cdek.ru/v2/oauth/token?grant_type=client_credentials&client_id=12345...&client_secret=adfc3

调用其他需要授权的接口时候如果headers Authorization中bearer字段把b字母小写的话、请求返回:

  1. {
  2. "timestamp": 1584501484081,
  3. "path": "/v2/webhooks",
  4. "status": 500,
  5. "error": "Internal Server Error",
  6. "message": "Значение JWT токена должно быть задано след. шаблоном: [Bearer] + [пробел] + [JWT токен]"
  7. }

message:提交的JWT值必须符合以下模板:[Bearer] + [空格] + [JWT令牌]