[英]OAUTH2 - Fitbit using requests_oauthlib
Context 上下文
I am writing a python/django application which accesses Fitbit data. 我正在编写一个访问Fitbit数据的python / django应用程序。
Problem 问题
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. Firstly I present the user with a link: 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. User goes to Fitbit, logs into their account and authorises access. 2.用户访问Fitbit,登录他们的帐户并授权访问。
3. The user is then returned to my site with a code that should allow me to get the token. 3.然后用户返回我的站点,其代码应该允许我获取令牌。
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()
)
Once callback_2
is called I get the error: callback_2
,我收到错误:
(missing_token) Missing access token parameter.
Resources : 资源 :
Found a way around this. 找到解决这个问题的方法。 Pretty simple after all the effort of research. 经过研究的全部努力,相当简单。 The following is a custom method using
requests.post()
andbase64.b64encode()
. 以下是使用requests.post()
和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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.