问题描述
我想使用Django中Google Plus登录的 python-social-auth
功能登录用户。从我的网站登录,一切正常,正确的详细信息被添加到数据库。
I want to login a user using the python-social-auth
functionality for Google Plus signin in Django. When logging in from my website, everything works fine and the correct details are added to the database.
但是,我想要从我的Android应用程序进行身份验证。用户登录应用程序,然后将访问令牌发送到django API,该进程处理以下代码中的登录过程,改编自:
However, I want to authenticate from my Android application as well. The user logs in in the application, which then sends the access token to the django API, which handles the login process in the following code, adapted from 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
表格包含:
When logging in from the website, the social_auth_usersocialauth
table contains:
id | provider | uid | extra_data
==========================================
10 | google-oauth2 | <myemail> | {"token_type": "Bearer", "access_token": "<token>", "expires": 3600}
但是,当使用上述功能从应用程序登录时,操作完成,但表中的条目如下所示:
However, when logging in from the application using the above function, the operation completes ok, but the entry in the table looks like this:
id | provider | uid | extra_data
=========================================
10 | google-oauth2 | <empty> | {"access_token": "", "expires": null}
另外, auth_user
表包含用户名
,如 eeed494412obfuscated48bc47dd9b
而不是Google Plus用户名, 电子邮件
字段为空。
Also, the auth_user
table contains a username
like eeed494412obfuscated48bc47dd9b
instead of the Google Plus username and the email
field is empty.
我做错了什么,我如何获得相同的功能我得到网站?
我想提到我已经从Android应用程序实施了Facebook和Twitter身份验证,这称为上述功能并存储正确的详细信息,只有Google Plus引起问题。
I would like to mention that I have implemented Facebook and Twitter authentication from the Android application, which call the above-mentioned function and store the correct details, only Google Plus is causing problems.
推荐答案
我终于想到了自己。根据,我还需要请求在Android应用程序中发出请求。一旦我添加了这个,$ python-social-auth 代码被设法将电子邮件正确存储在 uid
字段中。这允许它识别相同的用户,无论是从网站或应用程序登录,这是我需要的。以下是我使用的范围字符串:
I finally figured it out myself. According to this article in the Android's Google Plus documentation, I also need to request the plus.profile.emails.read
scope when making the request in the Android app. Once I added this, the python-social-auth
code managed to store the email properly in the uid
fields. This allows it to recognize the same user whether logging in from the website or the app, which is what I needed. Here's the scopes string I use:
String scopes = "oauth2:" + Plus.SCOPE_PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read";
但是, extra_data
字段仍然包含上面提到的值我相信这是因为需要离线访问,这将允许Google Plus将缺少的字段传回 python-django-auth
。 。
However, the extra_data
field still contains the values I mentioned above. I believe this is due to needing to request offline access as well, which would allow Google Plus to pass the missing fields back to python-django-auth
. More details can be found here.
这篇关于python-social-auth不正确的Google OAuth2详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!