使用 python-social-auth
,我在接受来自 google 的访问后收到 403: Forbiden
错误消息
编辑:我最近(2017 年)遇到了同样的错误,但有一条新消息:401 Client Error: Unauthorized for url: https://accounts.google.com/o/oauth2/token
此答案已过时,因为 Google+ API 将于 2019 年 3 月 7 日弃用
您需要将 Google+ API
添加到 Google Developer Console 上的启用 API 列表中(在 APIs
下)
注意:如果您想查看真正的错误消息,请使用回溯查看 response
变量 (response.text
) 的内容。为此,我使用 werkzeug (django-extensions
+ python manage.py runserver_plus
)。
还要感谢。我正在使用此 python-social-auth tutorial by art and logic,但无法通过 /complete/google-oauth2/ 处的 403:Forbidden HTTPError,直到按照上述方式启用 Google+ API并等待几分钟让 Google 启用它。
此外,我必须将模板放在模板目录中并在 settings.py 中设置TEMPLATE_DIRS = ('/path/to/psa_test/thirdauth/templates/',)
。
希望这对一路上的人有所帮助。总而言之,花了大约6个小时才弄清楚。还不错,我很开心。
对我来说,我使用的是从 2014 年 9 月 1 日起被 Google 弃用的完整 URI 范围,这里的 python-social-auth 文档中提到了这一点
http://psa.matiasaguirre.net/docs/backends/google.html#google-oauth2
自 2014 年 9 月 1 日起,Google 将弃用完整 URL 范围,转而支持 Google+ API 和最近引入的较短范围名称。但是 python-social-auth 已经在 v0.1.24 发布的 e3525187 中引入了范围更改。
但是,如果您出于任何原因不想启用 Google+ API 并希望继续使用完整 uri 旧范围,则需要按照同一链接中提到的步骤操作:
# Google OAuth2 (google-oauth2)
SOCIAL_AUTH_GOOGLE_OAUTH2_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
# Google+ SignIn (google-plus)
SOCIAL_AUTH_GOOGLE_PLUS_IGNORE_DEFAULT_SCOPE = True
SOCIAL_AUTH_GOOGLE_PLUS_SCOPE = [
'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
SOCIAL_AUTH_GOOGLE_OAUTH2_USE_DEPRECATED_API = True
SOCIAL_AUTH_GOOGLE_PLUS_USE_DEPRECATED_API = True
这对我有用,因为此时我不想启用 Google+ API。