python - 使用 Python social auth 只获取 token

标签 python django oauth-2.0

我正在使用神奇的 Python social auth使用 Django
但是,目前,每次调用该流程时,都会创建一个新用户。我只需要进程中的 token (access_tokenrefresh_token)。如何实现?通过某种管道?

这是我目前的 pipeline.py 代码(缩写):

def get_token(backend, user, response, *args, **kwargs):

    # get token from the oauth2 flow
    social = user.social_auth.get(provider='google-oauth2')
    access_token = social.extra_data['access_token']
    refresh_token = social.extra_data.get('refresh_token')

以及对应的settings.py文件:

# set django session
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

# psa settings
SOCIAL_AUTH_URL_NAMESPACE = 'social'

# see http://psa.matiasaguirre.net/docs/configuration/settings.html
SOCIAL_AUTH_UUID_LENGTH = 32

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

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',
    'youtube.pipeline.get_token',
)

最佳答案

是的,一切都在筹备中。如果查看已有的内容,您甚至会看到 social.pipeline.user.create_user 步骤。

From the documentation :

# Create a user account if we haven't found one yet.
'social.pipeline.user.create_user',

( source for that function )

将此(以及以下所有步骤,如果您不需要它们)替换为您想要实现的目标。

def get_token(backend, uid, *args, **kwargs):
    # get token from the oauth2 flow
    provider = backend.name
    social = backend.strategy.storage.user.get_social_auth(provider, uid)
    access_token = social.extra_data['access_token']
    refresh_token = social.extra_data.get('refresh_token')

即使没有用户存在/未创建,修改后的方法也应该有效。

关于python - 使用 Python social auth 只获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922535/

相关文章:

python - 找出给定颜色的互补色/相反色

python - 使用pymc在Python中进行内存溢出

javascript - 脚本在 django 模板中不起作用

javascript - 在 Web 应用程序加载时加载 JSON 响应(JQuery、Django)

python - WSGIScriptAlias 为/PREFIX 时的 Django localeURL

python - pandas 日期时间索引序列差异

django - 在不同版本的 Django 下运行两个 Django 应用程序

Spring OAuth2 Redis 客户端详细信息

java - SecureSocial 库中缺少 securesocial.core.Identity?

github - 如何为 GitHub 存储库设置环境变量?