python - social-auth-app-django:刷新 access_token

标签 python django python-3.x

我将 social-auth-app-django 用于我的 django 网站。
登录一切正常,但在 token 过期后。我不能再访问谷歌的用户数据了。
我找到了如何刷新 token ,但它给出了

 File "/mnt/s/github/nascentapp/app/booking/management/commands/sendmail.py", line 17, in handle
    new_token = self.get_token(user=booking_user, provider='google-oauth2')
  File "/mnt/s/github/nascentapp/app/booking/management/commands/sendmail.py", line 28, in get_token
    social.refresh_token(strategy)
  File "/home/sander/.local/share/virtualenvs/app-YMrBBUv3/lib/python3.6/site-packages/social_core/storage.py", line 58, in refresh_token
    response = backend.refresh_token(token, *args, **kwargs)
  File "/home/sander/.local/share/virtualenvs/app-YMrBBUv3/lib/python3.6/site-packages/social_core/backends/oauth.py", line 438, in refresh_token
    request = self.request(url, **request_args)
  File "/home/sander/.local/share/virtualenvs/app-YMrBBUv3/lib/python3.6/site-packages/social_core/backends/base.py", line 234, in request
    response.raise_for_status()
  File "/home/sander/.local/share/virtualenvs/app-YMrBBUv3/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.google.com/o/oauth2/token

这是我的一些代码
def get_token(self, user, provider):
        social = user.social_auth.get(provider=provider)
        print('This is social of user: ', social)
        if (social.extra_data['auth_time'] + social.extra_data['expires']) <= int(time.time()):
            print('\n Token is out of date \n')
            strategy = load_strategy()
            social.refresh_token(strategy)
        return social.extra_data['access_token']

在我的设置文件中:
AUTHENTICATION_BACKENDS = (
    'social_core.backends.open_id.OpenIdAuth',  # for Google authentication
    'social_core.backends.google.GoogleOpenId',  # for Google authentication
    'social_core.backends.google.GoogleOAuth2',  # for Google authentication
    'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ.get('DJANGO_SOCIAL_AUTH_GOOGLE_OAUTH2_KEY')  # Paste CLient Key
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ.get('DJANGO_SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET') # Paste Secret Key

SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
    'https://www.googleapis.com/auth/calendar.readonly',
    'https://www.googleapis.com/auth/calendar.events'
]

最佳答案

通过添加以下内容修复它:

SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {
    'access_type': 'offline',
    'approval_prompt': 'auto'
}

如果用户已经注册,则需要第一次强制提示(否则得不到刷新 token )
/login/google-oauth2?approval_prompt=force

关于python - social-auth-app-django:刷新 access_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62188851/

相关文章:

python - np.zeros 和 np.full 内存消耗和性能差异的原因

python - Django 使用对象自己的数据对多个更新进行建模?

python - Django:删除对象中的 m2m 关系

django 覆盖模型 save() 方法

python - 从大量 .txt 文件及其频率生成 Ngram(Unigrams、Bigrams 等)

python - 无法使用 GPU 在使用 P100-NC6s-V2 计算的 Azure 机器学习服务中训练 NN 模型。因 CUDA 错误而失败

django - 将collectstatic 与最新版本的Boto、管道和存储一起使用时,x-amz-acl 设置不正确

python-3.x - 通过python中的多个条件合并不同数量的行和列

python - 为什么 exec() 中的符号定义语句有时对本地符号表没有影响?

python - 在给定列表中创建重复元素的新列表