标签: python-social-auth

如何从 python-social-auth 获取后端实例

我目前使用python-社会身份验证,并增加了后端MyOAuth其中BaseOAuth1,内部BaseOAuth1类有一个方便的oauth_request,我想利用外部类的实例方法。

https://github.com/omab/python-social-auth/blob/master/social/backends/oauth.py

我尝试直接实例化该类,但看起来我缺少一些上下文。

有没有办法引用该MyOAuth后端实例?我期待类似的东西

request.user.social_auth.get(provider='MyOAuth').backend.oauth_request(...)

python django python-social-auth

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

如何避免这个"'元组'对象在我的自定义用户模块中没有属性'photo'"错误?

我正在尝试存储有关使用facebook登录到我的网站的用户的其他信息,因此我创建了一个UserProfile模型.

这是我定义UserProfile的方式:

models.py

from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    photo = models.TextField()

    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            UserProfile.objects.create(user=instance)

    post_save.connect(create_user_profile, sender=User)
Run Code Online (Sandbox Code Playgroud)

settings.py

AUTH_PROFILE_MODULE = 'blog.UserProfile'
Run Code Online (Sandbox Code Playgroud)

而且,由于我使用python-social-auth进行身份验证,我正在实现一个自定义管道,用于在UserProfile中存储用户的图像URL.

from blog.models import UserProfile

def get_profile_picture(
    strategy,
    user,
    response,
    details,
    is_new=False,
    *args,
    **kwargs
    ):
    img_url = 'http://graph.facebook.com/%s/picture?type=large' \
        % response['id']
    profile = UserProfile.objects.get_or_create(user = user)
    profile.photo = img_url
    profile.save()
Run Code Online (Sandbox Code Playgroud)

但是我得到了以下错误:'tuple'对象没有属性'photo'

我知道UserProfile具有属性"photo",因为这是该表的定义:

table|blog_userprofile|blog_userprofile|122|CREATE TABLE "blog_userprofile" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL UNIQUE …
Run Code Online (Sandbox Code Playgroud)

python django python-social-auth

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

Python Social Auth:如何定义不同的用户名方案?

Python Social Auth中的默认用户名生成方案是获取auth'd社交网络的用户名,如果已经采用,则向其添加一些sorta随机值(或者它是一个哈希?).

无论如何,我想改变那种行为,我想定义自己的用户名生成方法.例如用户名+提供者+随机.

想法?

python django django-socialauth python-social-auth

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

django 在某处引发异常

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.associate_by_email',
)
Run Code Online (Sandbox Code Playgroud)

通过使用上面的代码settings.py我可以避免...

(1062, "Duplicate entry 'example@example.com' for key 'email'")错误信息。

但我在网上搜索,发现这个方便的代码可以放入exception所需的 html 页面中:

[代码1]: #backends.py

class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        msg = None
        if #no duplicate email:
            return HttpResponse("# catched exception")
        else:
            # processing msg here
            return render_to_response(# html, {msg}, context)
Run Code Online (Sandbox Code Playgroud)

# 设置.py

MIDDLEWARE_CLASSES = (
    'frontend.backends.MySocialAuthExceptionMiddleware'
)
Run Code Online (Sandbox Code Playgroud)

我的问题是solved基于上面的代码。但之前我使用以下代码使用了另一种功能,它与上述概念完全不同。

[代码2]:

def function(request):
    #actual code here
    return HttpResponse('msg here')
Run Code Online (Sandbox Code Playgroud)

但是在运行上面的代码时,我收到了类似的错误消息,

tuple index out of range在这个MySocialAuthExceptionMiddleware..

实际上,对于上述代码来说,这不是正确的错误消息。该消息与“ [Code …

python django django-views python-social-auth

1
推荐指数
1
解决办法
2万
查看次数

使用Django的python-social-auth:ImportError:没有名为'openid.association'的模块

我正在尝试使用python-social-authDjango 1.9和Python 3.据我所知,我已经安装了所有必要的要求,并在我的所有必需的设置settings.py.但是,当我尝试运行迁移或运行Django dev服务器时,我收到以下错误:

ImportError: No module named 'openid.association'
Run Code Online (Sandbox Code Playgroud)

完整的追溯如下:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f6fe7ea5a60>
Traceback (most recent call last):
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ethan/.virtualenvs/flywithme/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate …
Run Code Online (Sandbox Code Playgroud)

python django python-openid python-3.x python-social-auth

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

给定OAuth2 access_token,如何授权Python Google API客户服务?

我已经在我的Django项目中为Google OAuth2 实现了python-social-auth库,并能够成功使用它登录用户。该库将access_token收到的信息存储在Google OAuth2流的响应中。

我的问题是:使用google-api-python-client似乎依赖于创建和授权credentials对象,然后使用它来构建API,service如下所示:

...
# send user to Google consent URL, get auth_code in response

credentials = flow.step2_exchange(auth_code)
http_auth = credentials.authorize(httplib2.Http())

from apiclient.discovery import build
service = build('gmail', 'v1', http=http_auth)

# use service for API calls...
Run Code Online (Sandbox Code Playgroud)

由于我是从access_tokenpython-social-auth提供的开始,因此如何service为未来的API调用创建和授权API客户端?

编辑:澄清一下,上面的代码来自Google提供的示例。

python oauth2client google-api-python-client python-social-auth google-oauth2

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

将 django_social 添加到 INSTALLED_APPS 时出现“TypeError: object of type 'map' has no len()”错误

我正在尝试实现 django 社交身份验证,并做到了这一点pip install social-auth-app-django,然后我继续将其添加social_django到我的INSTALLED_APPS. 之后,当我运行应用程序时,我收到以下错误(我正在提交整个回溯):

Traceback (most recent call last):
  File "c:\users\edgar\appdata\local\programs\python\python35\lib\threading.py", line 914, in _bootstrap_inner
    self.run()
  File "c:\users\edgar\appdata\local\programs\python\python35\lib\threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Edgar\venvs\internet-lead\lib\site-packages\django\apps\registry.py", line …
Run Code Online (Sandbox Code Playgroud)

python-social-auth

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