标签: python-social-auth

使用django-rest-framework和python-social-auth的Google身份验证

简短的问题使用DRF,python-social-auth和Angularjs登录可在Facebook上使用,但不能在Google上使用。

我正在构建一个django应用程序,除了本地存储的电子邮件/密码组合之外,该应用程序还需要使用户能够通过Facebook和Google进行注册/登录。其工作方式如下:

  1. 对于注册和登录,Angularjs都会使用各自的app_id来启动FB和google API(FB.init和gapi.auth2.init)。页面中包含了为此所需的所有js库。
  2. 根据用户的选择,两个页面都允许用户使用Facebook或google登录或输入电子邮件/密码组合。
  3. 所有必需的信息,包括它们,access_token都是通过FB或Google的API收集的。
  4. 当用户在填写完所有相关详细信息后提交表单时(注册要求他们输入一些其他数据),包括在内的数据access_token将通过AJAX请求发送到服务器。
  5. 在服务器端,基于类的视图LoginView和SignUpView接受请求并对其进行处理。该代码直接处理基于电子邮件的注册/登录。基于Google / FB的注册/登录传递给python-social-auth的do_auth函数进行身份验证。
  6. 当提供者选择为时facebook,这可以正常工作。何时Google(同时尝试google-oauth2google-plusdo_auth最终引发403禁止错误。当相关网址https://googleapis.com/plus/v1/people/me?access_token=ACCESS_TOKEN&alt=json复制到浏览器时,它会显示错误消息:

超出了未经身份验证的使用的每日限制。继续使用需要注册

  1. 我已确保以下几点:
    1. 在Google开发人员控制台中为相应应用程序启用了Google+ API
    2. 从google开发者控制台中,添加http://localhost:5001到javascript origin字段并http://localhost:5001/social/complete重定向uri字段(稍后填充该字段。无论有没有,结果相同。)
    3. 生成密钥,并将client_id和client_secret复制到settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEYsettings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET。仔细检查了它们的准确性。
    4. 已添加'social.backends.google.GoogleOAuth2'到设置。AUTHENTICATION_BACKENDS。试图将GoogleOAuth2和GooglePlus一起放置在此处,并分开放置。
    5. 还使用PLUS(而不是OAUTH2)在2-3中进行了所有这些尝试。结果还是一样。
    6. 设置settings.SOCIAL_AUTH_USE_DEPRECATED_API为True 后再次尝试。这也将失败,但是错误现在是401。

下一步该如何使用Google身份验证?遍历了许多其他类似的问题以及Github中报告的问题。

以下是相关代码:

class SignUpView(CreateAPIView):

    def create(self, request, *args, **kwargs):
        provider = request.data['provider']
        strategy = load_strategy(request)
        backend = load_backend(strategy=strategy, name=provider, redirect_uri=None)
        token = request.data['access_token']
        try:
            user = …
Run Code Online (Sandbox Code Playgroud)

django-rest-framework python-social-auth

5
推荐指数
1
解决办法
3525
查看次数

Python Social Auth获得Google头像

有没有办法使用python social auth获取Google个人资料图片网址?

所以,这就是我为facebook和twitter所做的事情,用这段代码添加一个管道:

    if strategy.backend.name == 'facebook':
        url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])

    elif strategy.backend.name == "twitter":
        if response['profile_image_url'] != '':
            url = response['profile_image_url']

    elif strategy.backend.name == "GoogleOAuth2":  # doesn't work
        user_id = response['id']
        url = "???"
Run Code Online (Sandbox Code Playgroud)

首先,我不知道后端的名称是什么,是"GoogleOAuth2"吗?第二,我应该用什么网址来保存个人资料的头像?这是这样的吗?

python google-login google-plus python-social-auth

4
推荐指数
1
解决办法
3293
查看次数

AttributeError:'DjangoStrategy'对象没有属性'backend'python social auth

这上周工作了.也许我做错了什么并搞砸了别的地方,或者它可能是一个bug,或者它只是一个更新而我在阅读文档时错过了它.

我有一个管道,可以获取用户的头像并保存URL:

def get_avatar(strategy, details, response, user, *args, **kwargs):
    url = None
    if strategy.backend.name == 'facebook':
        url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
    elif strategy.backend.name == "twitter":
        if response['profile_image_url'] != '':
            url = response['profile_image_url']
    elif strategy.backend.name == "google-oauth2":
        if response['image'].get('url') is not None:
            url = response['image'].get('url')
Run Code Online (Sandbox Code Playgroud)

它曾经工作,现在,它给了我错误:

 'DjangoStrategy' object has no attribute 'backend'
Run Code Online (Sandbox Code Playgroud)

请帮助,一些测试用户已经在使用我的网站,目前他们没有个人资料图片.

python django python-social-auth

4
推荐指数
2
解决办法
1670
查看次数

python social auth:谷歌登录错误:invalid_client

我正在使用python social auth在我的应用上实现谷歌登录.但是我收到以下错误

Error: invalid_client
The OAuth client was not found.
Run Code Online (Sandbox Code Playgroud)

我尝试添加与项目名称和电子邮件地址相同的产品名称.但我仍然得到这个错误

我在谷歌开发者控制台中重定向uri:

 http://localhost:8000/complete/google-oauth2/ 
Run Code Online (Sandbox Code Playgroud)

的login.html

a href="{% url 'social:begin' 'google-oauth2' %}?next={{ index }}"> | Login with Google</a>
Run Code Online (Sandbox Code Playgroud)

settings.py

AUTHENTICATION_BACKENDS = (
    'social.backends.facebook.FacebookOAuth2',
    'social.backends.twitter.TwitterOAuth',
    'social.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_GOOGLE_OAUTH_KEY = ''

SOCIAL_AUTH_GOOGLE_OAUTH_SECRET = ''
Run Code Online (Sandbox Code Playgroud)

python django google-oauth python-social-auth

4
推荐指数
1
解决办法
1813
查看次数

twitter登录:401客户端错误:需要授权

我正在使用python-social-auth在本地实现twitter登录,但我得到401客户端错误.我的django版本是1.6.

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/social/apps/django_app/utils.py" in wrapper
  45.             return func(request, backend, *args, **kwargs)
File "/Library/Python/2.7/site-packages/social/apps/django_app/views.py" in auth
  12.     return do_auth(request.social_strategy, redirect_name=REDIRECT_FIELD_NAME)
File "/Library/Python/2.7/site-packages/social/actions.py" in do_auth
  25.     return strategy.start()
File "/Library/Python/2.7/site-packages/social/strategies/base.py" in start
  66.             return self.redirect(self.backend.auth_url())
File "/Library/Python/2.7/site-packages/social/backends/oauth.py" in auth_url
  99.         token = self.set_unauthorized_token()
File "/Library/Python/2.7/site-packages/social/backends/oauth.py" in set_unauthorized_token
  158.         token = self.unauthorized_token()
File "/Library/Python/2.7/site-packages/social/backends/oauth.py" in unauthorized_token
  177.                                 method=self.REQUEST_TOKEN_METHOD)
File "/Library/Python/2.7/site-packages/social/backends/base.py" in request
  205.         response.raise_for_status()
File "/Library/Python/2.7/site-packages/requests/models.py" in raise_for_status
  808.             raise HTTPError(http_error_msg, response=self) …
Run Code Online (Sandbox Code Playgroud)

python django twitter-oauth python-social-auth

4
推荐指数
1
解决办法
3851
查看次数

强制谷歌通过python社会认证刷新令牌

我们正在开发一个依赖于PSA(0.2.1)的项目,用于使用google oauth2(离线)进行身份验证.不知怎的,我们失去了一些用户的刷新令牌,我们想强制这些用户重新认证,这样我们就可以从谷歌获得新的刷新令牌

我们都试过了:

  1. Diconnect使用/ diconnect /谷歌的oauth2的用户,我们得到了一个NotAllowedToDisconnect例外,甚至删除后social.pipeline.disconnect.allowed_to_disconnectSOCIAL_AUTH_DISCONNECT_PIPELINE,我们有没有异常,但是当用户重新认证,有在谷歌的反应没有refreh_token
  2. 将approval_prompt = force添加到'account:social:begin'url,但不返回refresh_token

任何想法都将受到高度赞赏.

更新:我们尝试使用{% url 'account:social:begin' 'google-oauth2' %}?approval_prompt=force&next=/强制某些用户(缺少令牌)的approval_prompt,但它似乎对google oauth没有任何影响.

谢谢

django oauth-2.0 google-oauth python-social-auth

4
推荐指数
1
解决办法
1376
查看次数

python social auth重复条目

我正在使用django的python-social-auth.我想通过Facebook授权,但之前,我已经使用facebook使用的电子邮件以默认方式(电子邮件,密码)注册.我可以将Facebook帐户与注册帐户相关联吗?我试图搜索,但没有

django python-social-auth

3
推荐指数
1
解决办法
1195
查看次数

python-social-auth 和 github,我遇到此错误“redirect_uri 必须与此应用程序的已注册回调 URL 匹配”

我在一个项目中使用 python-social-auth 通过 Github 对用户进行身份验证。我需要根据用户使用的链接重定向用户。为此,我在 url 上使用了 next 属性,并且我没有在 github 应用程序上声明任何重定向 url,也没有在 django 设置中声明任何重定向 url。

这是我用于链接的 href 属性:{% url 'social:begin' 'github' %}?next={% url 'apply' j.slug %}

当我第一次点击它时,我被重定向到我的主页,网址字段中出现此错误:http://127.0.0.1:8000/?error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.&error_uri=https%3A%2F%2Fdeveloper.github.com%2Fv3%2Foauth%2F%23redirect-uri-mismatch&state=Ui1EOKTHDhOkNJESI5RTjOCDEIdfFunt

但第一次之后链接就工作了。

我不知道问题出在哪里,希望有人能帮助我。谢谢

python authentication django github python-social-auth

3
推荐指数
1
解决办法
5947
查看次数

使用django python-social-auth重定向后会话值丢失

我正在使用python-social-auth对django项目进行身份验证.我正在localhost上运行django服务器并让facebook设置我的应用程序重定向到http://127.0.0.1:8000/complete/facebook/,它开始python-social-auth的管道来验证用户.我使用postgres作为我的数据库.

调用此方法并尝试进行身份验证时,无法找到有关会话的信息.从https://github.com/omab/python-social-auth/issues/534,我认为sessionid cookie正在被覆盖.如果我将facebook重定向发送到另一个网址以加载没有身份验证的静态页面,则没有错误,但我也没有验证或从Facebook获取任何信息.

我怎么会不覆盖sessionid cookie-当然,这是实际的问题 - 还是我可能在这里缺少另一个问题?

[03/Jun/2016 05:19:58] "GET /login/facebook/?next=/lithium-web/ HTTP/1.1" 302 0
Internal Server Error: /complete/facebook/
Traceback (most recent call last):
  File "/Users/mac/Desktop/lithium-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mac/Desktop/lithium-web/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mac/Desktop/lithium-web/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/mac/Desktop/lithium-web/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mac/Desktop/lithium-web/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 51, in wrapper
    return func(request, backend, *args, **kwargs)
  File …
Run Code Online (Sandbox Code Playgroud)

authentication django localhost session-cookies python-social-auth

3
推荐指数
1
解决办法
3318
查看次数

Python Social Auth为不同用户复制电子邮件

在我的网站上,可以通过以下方式登录:

  • 用户名和密码
  • 电子邮件和密码
  • 谷歌授权 2
  • Facebook

我在哪里使用系统内置的 django 用户和 python 社交身份验证。

问题:

假设我创建了以下帐户:

用户名:losimonassi 电子邮件:lorenzosimonassi@gmail.com

然后,当我尝试使用我的 gmail (lorenzosimonassi@gmail.com) 登录时,python social auth 会使用相同的电子邮件创建另一个用户。因此,当我尝试使用我的电子邮件登录时,身份验证系统会发现两个类似的电子邮件,这会引发错误。

我试图找到一种方法,当用户尝试使用 gmail 登录时,会根据数据库检查他的电子邮件,如果它已经存在,则通过重定向和警报消息停止进程(我认为可以通过中间件)。

但是当然对DB的检查应该只对其他后端用户和普通用户进行检查,以免阻塞自己的登录。

我不想关联帐户。

设置.py

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    #'social.pipeline.social_auth.associate_user',
    #'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)
Run Code Online (Sandbox Code Playgroud)

管理员形象

django django-authentication python-social-auth

3
推荐指数
2
解决办法
1964
查看次数