django - 使用 django social auth GOOGLE_OAUTH2 获取电话号码
问题描述
我想在使用 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',
)
在管道.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'}
我遵循了文档配置-
在谷歌身份验证页面中,它要求允许view phone numbers
,但作为回应我没有得到电话号码,我在做什么错误?
解决方案
您只能获取此页面上显示的电话号码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
推荐阅读
- laravel - Laravel VUEJS axios如何将数组插入数据库
- java - 如何通过 selenium java 中的断言来验证动态表
- mysql - MariaDB: ERROR 2006 (HY000): MySQL server has gone away
- flask - 将散景图部署到服务器
- c# - In C#, how to set all tree nodes checked true just after treeview loaded
- c# - 为什么我的 powershell 函数无法从 C# 调用?
- knime - Knime MongoDBReader 读取一个包含数组的对象
- reactjs - 没有访问控制源API axios
- python-3.x - 将代理对转换为表情符号 - python3
- sympy - 如何在 sympy 中禁用不必要的括号?