【发布时间】:2015-03-30 01:07:01
【问题描述】:
我想在 Django 中使用 python-social-auth 功能登录用户以进行 Google Plus 登录。从我的网站登录时,一切正常,并且正确的详细信息已添加到数据库中。
但是,我也想通过我的 Android 应用程序进行身份验证。用户登录应用程序,然后将访问令牌发送给 django API,django API 处理登录过程如下代码,改编自the documentation:
@csrf_exempt
@serengeti_api_request
@psa('social:complete')
def login_social_token(request, backend):
# Ensure the token has been specified.
token = request.META.get('HTTP_ACCESSTOKEN')
if token is None:
raise SerengetiApiRequestException('Access token is missing!')
# Login the user for this session
user = request.backend.do_auth(token)
if user is None:
raise SerengetiApiRequestException('Could not authenticate user!')
login(request, user)
# Store the email address if one has been specified (e.g. Twitter)
email = request.META.get('HTTP_EMAIL')
if email is not None:
user.email = email
user.save()
# Prepare the parameters to be returned
response = dict({
'id': user.id,
'first_name': user.first_name,
'last_name': user.last_name,
'api_key': request.session.session_key,
})
# Return a 200 status code to signal success.
return HttpResponse(json.dumps(response, indent=4), status=200)
从网站登录时,social_auth_usersocialauth 表包含:
id | provider | uid | extra_data
==========================================
10 | google-oauth2 | <myemail> | {"token_type": "Bearer", "access_token": "<token>", "expires": 3600}
但是,当使用上述功能从应用程序登录时,操作完成,但表中的条目如下所示:
id | provider | uid | extra_data
=========================================
10 | google-oauth2 | <empty> | {"access_token": "", "expires": null}
此外,auth_user 表包含一个 username(如 eeed494412obfuscated48bc47dd9b)而不是 Google Plus 用户名,email 字段为空。
我做错了什么?如何才能获得与网站上相同的功能?
我想提一下,我已经从 Android 应用程序中实现了 Facebook 和 Twitter 身份验证,它们调用了上述函数并存储了正确的详细信息,只有 Google Plus 引起了问题。
【问题讨论】:
-
嗨,你能分享完整的代码吗
标签: django python-2.7 django-socialauth google-login python-social-auth