问题描述
我正在尝试使用本文档中所述的显式授权流程检索 Yahoo API 的访问令牌:https://developer.yahoo.com/oauth2/guide/flows_authcode
I am trying to retrieve the access token for the Yahoo API, using the explicit grant flow as described in this document:https://developer.yahoo.com/oauth2/guide/flows_authcode
一切正常,直到第 4 步:交换访问令牌的授权码
我编写了以下python脚本来检索代码:
I wrote the following python script to retrieve the code:
import urllib2
import requests
import json
url = 'https://api.login.yahoo.com/oauth2/get_token'
body = "grant_type=authorization_code&redirect_uri=oob&code=************"
headers = {
'Authorization': 'Basic **************',
'Content-Type': 'application/json'
}
r = requests.post(url, data=body, headers=headers)
print r
注意:我用****"替换了敏感数据
Note: I replaced sensitive data with "****"
现在,当我执行脚本时,我只收到401"错误消息.
Now, when I execute the script, I only get the "401" error message.
我 100% 确定登录凭据没有问题,所以这似乎与我提出请求的方式有关.这也是我第一次在 python 中使用请求".
I am 100% sure that the login credentials are fine, so it seems to be related to the way I make the request. It's also the first time that I am using "requests" in python.
如果您能给我一些关于代码的反馈,以及我是否正确传递标头和正文信息,那就太好了.我特别不确定尸体的通过.该文档仅说明以下内容:
Would be great, if you could give me some feedback on the code, and if I am passing the header and body information correctly. I am especially unsure about the passing of the body. The documentation only states the following:
示例请求正文:grant_type=authorization_code&redirect_uri=https%3A%2F%2Fwww.example.com&code=abcdef
推荐答案
将你的 body 变量更改为 dict,即,
Change your body variable to a dict, i.e.,
body = {
'grant_type': 'authorization_code',
'redirect_uri': 'oob',
'code': '************',
}
不需要其他更改.希望有帮助.
No other changes are needed. Hope it helps.
这篇关于使用 OAuth 2.0 和 Python 请求检索 Yahoo API 的访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!