2016-04-28 14 views
10

Tôi đang sử dụng tuyệt vời Python social auth với Django.
Tuy nhiên, tại thời điểm này, mọi quá trình được gọi, người dùng mới được tạo. Tôi chỉ cần mã thông báo (access_tokenrefresh_token) từ quy trình. Làm thế nào điều này có thể đạt được? Thông qua một số loại đường ống?Sử dụng xác thực xã hội Python để chỉ nhận mã thông báo

Đây là pipeline.py mã của tôi tại thời điểm (viết tắt):

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 tập tin tương ứng:

# 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', 
) 
+0

* "... mọi khi quá trình được gọi, người dùng mới được tạo ..." * - quy trình nào? – xyres

+0

@xyres: Quá trình xác thực người dùng đối với Google+ hoặc Facebook hoặc Twitter. Nó được bắt đầu với một liên kết trong mẫu. – Jan

Trả lời

4

Vâng, đó là tất cả trong các đường ống. Nếu bạn nhìn vào những gì bạn đã có, thậm chí bạn sẽ thấy bước 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)

Thay thế này (và tất cả các bước sau đây nếu bạn không cần đến chúng) với bất cứ điều gì mà bạn đang cố gắng để Hoàn thành.

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') 

Phương pháp được sửa đổi sẽ hoạt động ngay cả khi không có người dùng nào tồn tại/được tạo.

+0

Tôi đi và thử cái này. – Jan

+0

Tôi gặp lỗi khi 'uid' không được xác định - làm cách nào để nhận được thông báo này? – Jan

+0

Ah, xin lỗi, có vẻ như ['uid' là một trong những đối số của hàm] (https://github.com/MSOpenTech/python-social-auth/blob/master/social/pipeline/social_auth.py#L18) (được tạo bởi bước 'social.pipeline.social_auth.social_uid'). Đã cập nhật bài đăng. – Anonymous

Các vấn đề liên quan