我正在尝试将Yahoo的API用于幻想足球。我最初可以接收访问令牌并刷新令牌,但是一旦该访问令牌过期,我将无法再获得另一个。
我的代码如下:
from requests import Request, get, post
import webbrowser
import base64
baseURL = 'https://api.login.yahoo.com/'
oauthENDPOINT = "https://api.login.yahoo.com/oauth2/request_auth"
## Generate a url using the endpoint and parameters above
params = {'client_id' : client_id,
'redirect_uri' : "oob",
'response_type' : 'code'}
p = Request('GET', oauthENDPOINT, params=params).prepare()
webbrowser.open(p.url)
最后一行将我发送到Yahoo网站,允许我自己访问和接收authCode
。
encoded = base64.b64encode((client_id + ':' + client_secret).encode("utf-8"))
headers = {
'Authorization': f'Basic {encoded.decode("utf-8")}',
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
'grant_type': 'authorization_code',
'redirect_uri': 'oob',
'code': authCode}
tokenResponse = post(baseURL + 'oauth2/get_token', headers=headers, data=data)
tokenResponseJSON = tokenResponse.json()
access_token = tokenResponseJSON['access_token']
refresh_token = tokenResponseJSON['refresh_token']
我现在拥有检查联盟设置的所有必要信息(例如)。
fbURL = 'https://fantasysports.yahooapis.com/fantasy/v2'
leagueURL1 = f'{fbURL}/leagues;league_keys=nfl.l.{leagueID}/settings'
headers = {
'Authorization': f'Bearer {access_token}',
'Accept': 'application/json',
'Content-Type': 'application/json'
}
response2 = get(leagueURL1, headers=headers,params={'format': 'json'})
上述工作正常。但是,access_token
会持续3600秒,并且一旦过期,我将无法使用refresh_token
来请求新的accessTokenData = {
'grant_type': 'refresh_token',
'redirect_uri': 'oob',
'code': authCode,
'refresh_token': refresh_token
}
accessTokenResponse = post(baseURL + 'oauth2/get_token', headers=headers, data=accessTokenData)
accessTokenJSON = accessTokenResponse.json()
。我的尝试:
access_token
在上面,我希望收到一个新的accessTokenJSON
,但是{'error': {'localizedMessage': 'client request is not acceptable or not supported',
'errorId': 'INVALID_INPUT',
'message': 'client request is not acceptable or not supported'}}
是这样的:
yahoo_oauth
到目前为止,我一直在关注these steps,到目前为止效果不错。我究竟做错了什么?我了解许多Python用户使用rauth
或client_id
进行身份验证,但这涉及将client_secret
和.json
分别保存在refresh_token
文件中,我正在寻找以动态加载它们。我认为我离成功并不遥远,但是在生成新的resource.data.updatedByHistoryArray.hasOnly([request.auth.uid])
方面我只是缺少一些东西。所有帮助非常感谢!