gpt4 book ai didi

python - OAUTH2 - Fitbit 使用 requests_oauthlib

转载 作者:行者123 更新时间:2023-11-30 23:10:25 27 4
gpt4 key购买 nike

上下文

I am writing a python/django application which accesses Fitbit data.

问题

To access a users data I must get their token for which can be used repeatably to access fitness data. The following goes through the current steps.

<小时/>

<强>1。首先,我向用户提供一个链接:

def register_2(request):
if request.user.is_authenticated():

oauth = OAuth2Session(
auth_2_client_id,
redirect_uri = redirect_uri,
scope = fitbit_scope)

authorization_url, state = oauth.authorization_url(fitbit_url_authorise_2)

return render_to_response('register.html',{
"authorization_url" : authorization_url
},context_instance=RequestContext(request))
return HttpResponseRedirect("/")
<小时/>

<强>2。用户访问 Fitbit,登录其帐户并授权访问。

<小时/>

<强>3。然后,用户将返回到我的网站,并带有一个代码,该代码应该允许我获取 token 。

def callback_2(request):
if request.user.is_authenticated():
code = request.GET.get('code')
state = request.GET.get('state')

oauth = OAuth2Session(
client_id = auth_2_client_id,
redirect_uri = redirect_uri
)

token = oauth.fetch_token(
code = code,
token_url = fitbit_url_access_2,
authorization_response = request.build_absolute_uri()
)

一旦调用callback_2,我就会收到错误:

(missing_token) Missing access token parameter.

资源:

Fitbit OAUTH2 API

OAuth2Session Docs

最佳答案

Found a way around this. Pretty simple after all the effort of research. The following is a custom method using requests.post() and base64.b64encode().

def callback_2(request):
if request.user.is_authenticated():
code = request.GET.get('code')
state = request.GET.get('state')

url = fitbit_url_access_2
data = "client_id=" + auth_2_client_id + "&" +\
"grant_type=" + "authorization_code" + "&" +\
"redirect_uri=" + redirect_uri_special + "&" +\
"code=" + code

headers = {
'Authorization': 'Basic ' + base64.b64encode(auth_2_client_id + ':' + consumer_secret),
'Content-Type': 'application/x-www-form-urlencoded'}

r = requests.post(url, data=data, headers=headers).json()

关于python - OAUTH2 - Fitbit 使用 requests_oauthlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com