简体   繁体   English

Box API通过python获取access_token

[英]Box API get access_token through python

Thanks in ahead for your help. 感谢您的帮助。 I'm getting authorization code through exactly this website on the readme page: https://app.box.com/api/oauth2/authorize?response_type=code&client_id=MY_CLIENT_ID&state=security_token%3DKnhMJatFipTAnM0nHlZA 我正通过自述页面上的此网站获取授权代码: https : //app.box.com/api/oauth2/authorize? response_type = code & client_id = MY_CLIENT_ID & state = security_token% 3DKnhMJatFipTAnM0nHlZA

And POST for access_token through python. 并通过python发布access_token But receive invalid_request error. 但是会收到invalid_request错误。 Same result as I try through postman. 与尝试邮递员的结果相同。 Attached my code and the error msg, did I understand something wrong? 附加了我的代码和错误消息,我是否理解错了?

url = 'https://api.box.com/oauth2/token'
payload = {'grant_type':'authorization_code','code':auth_code,'client_id':'ID','client_secret':'SECRET'}
r = requests.post(url, data=json.dumps(payload))
json_r = r.json()
print json_r

Error message: 错误信息:

{
  "error": "invalid_request",
  "error_description": "Invalid grant_type parameter or parameter missing"
}

The problem is that you encode the POST payload as a JSON-string when the requests.post function's data parameter expects an ordinary dictionary. 问题在于,当requests.post函数的data参数需要普通字典时,您会将POST负载编码为JSON字符串。 So just doing it like this should work: 因此,只需要这样做:

r = requests.post(url, data=payload)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM