【问题标题】:Get phone number with django social auth GOOGLE_OAUTH2使用 django social auth GOOGLE_OAUTH2 获取电话号码
【发布时间】:2020-04-01 11:20:19
【问题描述】:

我想在使用 GOOGLE_OAUTH2 登录时保存电话号码,为此我设置了以下设置 -

SOCIAL_AUTH_GOOGLE_OAUTH2_IGNORE_DEFAULT_AX_ATTRS = True
SOCIAL_AUTH_GOOGLE_OAUTH2_AX_SCHEMA_ATTRS = [
    ('phonenumber', 'phonenumber')
]

SOCIAL_AUTH_GOOGLE_OAUTH2_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
    'https://www.googleapis.com/auth/plus.login',
    'https://www.googleapis.com/auth/userinfo.profile',
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/user.phonenumbers.read',
]

用于管道 -

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.social_auth.associate_by_email',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'accounts.pipelines.demo_pipeline',
)

在 pipelines.py 中

def demo_pipeline(backend, details, response, *args, **kwargs):
    print(dict(backend.get_user_details(response), **details))
    print(kwargs)
    print(args)
    print(response)
    print(details)

通过打印语句,我试图查看电话号码,但电话号码不在其中,它会打印 -

{'first_name': 'Vikash', 'email': 'vikash.ch120@gmail.com', 'fullname': 'Vikash chaudhary', 'username': 'vikash.ch120', 'last_name': 'chaudhary'}
{'new_association': True, 'request': <AsgiRequest: GET '/oauth/complete/google-oauth2/?state=ogEhGag7Awmo0d2t9UruAGNuxQwZClGq&code=4/uAGpCv3mpSKzDnwYbZjhn39uOTe6ysYjN0ybJaukLiUo7aMRVSx8SWrnAz7IUZpl-Vps1DzQ4qgewgR8O0Z0XBU&scope=email+profile+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/user.phonenumbers.read&authuser=0&session_state=138e7b111722948cadba63fe63b20db5bd036801..a701&prompt=none'>, 'strategy': <social_django.strategy.DjangoStrategy object at 0x000002A0E0693780>, 'social': <UserSocialAuth: vikash.ch120@gmail.com>, 'pipeline_index': 10, 'username': 'vikash.ch120', 'user': <User: vikash.ch120@gmail.com>, 'storage': <class 'social_django.models.DjangoStorage'>, 'is_new': True, 'uid': 'vikash.ch120@gmail.com'}
()
{'access_token': 'ya29.ImW0B1jqyIaKlz-YTU85Ana3ggCb9jFSdBHf15WilEwlOYmjcQBvjvmmFsLYbDtPRz_k2Lv6hzixwF_H4gcjOSZkMaLpgsT5tKBwqzEGQ-OaxzMnC9p_AcUfe1vzmQK7J0byx6J_aA', 'sub': '113443706233393763678', 'given_name': 'Vikash', 'email': 'vikash.ch120@gmail.com', 'family_name': 'chaudhary', 'picture': 'https://lh3.googleusercontent.com/-YAzJ9fQQVHI/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rdnxxIBtM0_TxffVU_Lrt0o2cYO6g/photo.jpg', 'locale': 'en', 'token_type': 'Bearer', 'email_verified': True, 'expires_in': 3599, 'name': 'Vikash chaudhary', 'scope': 'https://www.googleapis.com/auth/user.phonenumbers.read openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email', 'id_token': 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjViNWRkOWJlNDBiNWUxY2YxMjFlMzU3M2M4ZTQ5ZjEyNTI3MTgzZDMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXpwIjoiNjgzODY4OTU2NzM1LXBkaWczYXI2cTUxdXZudmZrZ2hodGpmNW1kc2tubDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiNjgzODY4OTU2NzM1LXBkaWczYXI2cTUxdXZudmZrZ2hodGpmNW1kc2tubDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEzNDQzNzA2MjMzMzkzNzYzNjc4IiwiZW1haWwiOiJ2aWthc2guY2gxMjBAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJGanJHZzZKTmN6OXhKQkx5X0lPXzZBIiwiaWF0IjoxNTc1NzUzNTYzLCJleHAiOjE1NzU3NTcxNjN9.w_VlokapGAs52NhjSft36lz_xhNC95m2W6HPN4FxZ8x-iG-rs6LlfCkbej8uq6zsve3qZJvco4KCQmPZUSMebk8mf3t_S9cWLHupifZDFb7nHqhW3w7R_DfZ-N6MmVoikhGtpEq2nHIcHwB1zKlQ21u7cJ8nHvcltrgCO9iXBOq18rNobfHAG1yxZaqqJg_OsvXUN5AYhxDdRq2nzMvAbj1vuq7J1h5FpDjuEusWrwwYHDZ5lnqw0mYkfpfK1zVzr9ZIK8QfyXXNDTvj8bML5LMFLL-f0hku5d73OCC7QeeffRvK411V55vs0nC0zkOYN-lqLR3DXm7gkGGGPzHiNw'}
{'fullname': 'Vikash chaudhary', 'first_name': 'Vikash', 'username': 'vikash.ch120', 'last_name': 'chaudhary', 'email': 'vikash.ch120@gmail.com'}

我按照documentation的配置——

在 Google 身份验证页面中,它要求允许 view phone numbers,但作为回应,我没有得到电话号码,我在做什么错误?

【问题讨论】:

    标签: django oauth-2.0 google-oauth python-social-auth django-socialauth


    【解决方案1】:

    您只能获取此页面上显示的电话号码https://myaccount.google.com/u/1/profile

    您可以通过请求此链接获取电话号码:https://people.googleapis.com/v1/people/me?personFields=phoneNumbers

    代码示例:

    def demo_pipeline(backend, details, response, *args, **kwargs):
        phone = None
        user = kwargs.get('user')
        social = user.social_auth.get(provider='google-oauth2')
        response = requests.get(
            'https://people.googleapis.com/v1/people/me?personFields=phoneNumbers',
            params={'access_token': social.extra_data['access_token']}
        )
        data = json.loads(response.text)
    
        if 'phoneNumbers' in data and data['phoneNumbers']:
            phone = data['phoneNumbers'][0]['canonicalForm']
        return phone
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 2013-09-02
      • 1970-01-01
      相关资源
      最近更新 更多