使用OAuth 2.0和Python请求检索雅虎API访问令牌 [英] Retrieve access token for Yahoo API using OAuth 2.0 and Python requests
问题描述
我想检索的雅虎API的访问令牌,使用本文档中描述的明确授权流程:
https://developer.yahoo.com/oauth2/guide/flows_auth$c$c
一切都很好,直到第四步:交易所授权code的访问令牌
我写了下面的python脚本来检索code:
进口的urllib2
进口要求
进口JSONURL ='https://api.login.yahoo.com/oauth2/get_token
身体=grant_type = authorization_ code&放; REDIRECT_URI = OOB&放大器; code = ************
标题= {
'授权':'基本**************',
内容类型:应用/ JSON
}R = requests.post(URL,数据=人体,头=头)
打印内容R
请注意:我换成敏感数据****
现在,当我执行脚本,我只得到了401错误消息。
我确信,登录凭据都很好,所以它似乎与我发出请求的方式100%。这也是第一次,我在python使用请求。
将是巨大的,如果你可以给我的code一些反馈,如果我正确地传递标题和正文信息。我对身体的传球特别是不确定。该文件只规定如下:
样品请求正文:grant_type = authorization_ code&放; REDIRECT_URI = HTTPS%3A%2F%2Fwww.example.com&放大器; code = ABCDEF
块引用>解决方案你的身体变量更改为一个字典,即,
=身体{
grant_type':'authorization_ code',
REDIRECT_URI':'OOB',
code':'************',
}不需要进行其他的变化。希望它帮助。
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
Everything is fine until Step 4: Exchange authorization code for Access Token
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 "****"
Now, when I execute the script, I only get the "401" error message.
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:
Sample Request Body: grant_type=authorization_code&redirect_uri=https%3A%2F%2Fwww.example.com&code=abcdef
解决方案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请求检索雅虎API访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!