使用Python社交身份验证只能获取令牌

时间:2016-04-28 18:19:06

标签: python django oauth-2.0

我正在使用Djangoaccess_token。{ 但是,此时,每次调用该进程时,都会创建一个新用户。我只需要来自流程的令牌(refresh_tokenpipeline.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', ) 文件:

<dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>${myfaces.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-impl</artifactId>
    <version>${myfaces.version}</version>
</dependency>

1 个答案:

答案 0 :(得分:4)

是的,它全部在进行中。如果您查看已有的内容,您甚至会看到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')

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