简体   繁体   English

调试django-allauth社交网络登录失败

[英]Debugging django-allauth Social Network Login Failure

When using django-allauth to do an OAuth login via a social provider, sometimes it fails with the error page "Social Network Login Failure".使用 django-allauth 通过社交提供商进行 OAuth 登录时,有时会失败并显示错误页面“社交网络登录失败”。 There is no log output containing more information.没有包含更多信息的日志 output。 There is a feature request for this log output ( https://github.com/pennersr/django-allauth/issues/1120 ) but it has been open for over a year.此日志 output ( https://github.com/pennersr/django-allauth/issues/1120 ) 有一个功能请求,但它已经开放了一年多。 In the mean time, how do I get more information to debug this error?同时,如何获取更多信息来调试此错误?

More information is passed to the context used to render the error template but is not used in the default template. 更多信息将传递到用于呈现错误模板的上下文,但不会在默认模板中使用。

You can get log output by overriding the template and including in your template the following: 您可以通过覆盖模板并在模板中包含以下内容来获取日志输出:

{{ auth_error }}

or alternatively: 或者:

Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}

To override the template, add a folder to your Django template DIRS. 要覆盖模板,请将文件夹添加到Django模板DIRS。 In Django 1.8+, this looks like the following: 在Django 1.8+中,这看起来像如下:

TEMPLATES = [
    {
        ...
        DIRS: [os.path.join(BASE_DIR, 'templates')]
    }
]

Then, in that folder, make directory socialaccount and put in it a file called authentication_error.html 然后,在该文件夹中,创建目录socialaccount并在其中放入一个名为authentication_error.html的文件

@Zags answers is great and pointed me in the right direction. @Zags答案很棒,并指出了我正确的方向。

Building on that answer, I suggest start by copying the default authentication_error.html to your tree, eg: 基于该答案,我建议首先将默认的authentication_error.html复制到您的树中,例如:

$ find /wherever/your/python/is -name socialaccount
$ cp .../that/dir/socialaccount/authentication_error.html \
     ./myapp/templates/allauth/socialaccount/

Then add in to the copy of the template: 然后添加到模板的副本:

<p>
Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}
</p>

(Technically you could just edit the installed python version...) (从技术上讲,你可以编辑已安装的python版本......)

Result for me, slightly formatted: 我的结果,略有格式:

Code: unknown, 
Error: Error retrieving access token: 
b'{"error":{
  "message":"This IP can\'t make requests for that application.",
  "type":"OAuthException",
  "code":5,
  "fbtrace_id":"..."
}}'

"Social Network Login Failure" can occur due to multiple reasons but URL mismatch is the most frequent problem I have experienced: “社交网络登录失败”可能由于多种原因而发生,但URL不匹配是我遇到的最常见问题:

  1. If you are facing this problem while hosting on third party, do remember to change expected URL to host's url 如果您在托管第三方时遇到此问题,请务必将预期的URL更改为主机的URL

  2. If the problem is occuring during development only, you must have registered on social website using localhost or 127.0.0.1 . 如果仅在开发期间出现问题,则必须使用localhost或127.0.0.1在社交网站上注册。 Do call social website through correct url. 通过正确的网址打电话给社交网站。 For eg facebook doesn't allow to register 127.0.0.1 but localhost and "manage.py runserver" gives 127.0.0.1 as default. 例如,facebook不允许注册127.0.0.1,但localhost和“manage.py runserver”默认为127.0.0.1。 So open your website using "localhost". 因此,使用“localhost”打开您的网站。

Also ensure that you have added correct url to 'sites' in django admin and it has been correctly added to 'social application' 还要确保您已在django admin中为“网站”添加了正确的网址,并且已将其正确添加到“社交网络应用程序”中

This also happened to me when I was checking https site on http (if I could redirect production). 当我在http上检查https网站时(如果我可以重定向生产),这也发生在我身上。 Error code/message was empty. 错误代码/消息为空。
I commented out these for the http testing: 我为http测试注释了这些:

# SESSION_COOKIE_SECURE = True
# CSRF_COOKIE_SECURE = True
# APPEND_SLASH = True
# PREPEND_WWW = True

When I was testing locally, my problem was two fold: First, I had to make sure that I started the server using 当我在本地测试时,我的问题有两个:第一,我必须确保我使用了启动服务器

python manage.py runserver localhost:8000

Then, in the admin page I had to make sure the Sites table had an entry for localhost:8000, eg at 然后,在管理页面中,我必须确保Sites表有一个localhost:8000条目,例如at

http://localhost:8000/en/admin/sites/site/

The gotcha for me was that I'd had multiple attempts at this and the result was the the ID for my localhost:8000 Sites table entry had become greater than 1. You'll notice in the allauth documentation that is says to to have SITE_ID = 1 in the settings.py but if you inadvertently create a different ID for the entry, you'll have to either alter the database entry's ID or change SITE_ID value. 对我来说,问题是我在这方面有多次尝试,结果是我的localhost的ID:8000 Sites表条目已经大于1.你会注意到在allauth文档中说有SITE_ID settings.py中= 1但如果您无意中为条目创建了不同的ID,则必须更改数据库条目的ID或更改SITE_ID值。

The other issue I had (unrelated to the poster's issue) was that I also had old registrations url template include interfering with the allauth ones. 我遇到的另一个问题(与海报的问题无关)是我也有旧的注册网址模板包括干扰allauth的模板。

If you use a custom SocialAccountAdapter (can be set in your settings.py, read more here ) then you can simply overwrite the function authentication_error to log all of the errors. 如果您使用自定义SocialAccountAdapter (可以在settings.py中设置, 请在此处阅读更多内容 ),那么您只需覆盖函数authentication_error即可记录所有错误。

The function signature ( source here ) looks like this: 函数签名( 这里的源代码 )如下所示:

(main/wherever.py): (主/ wherever.py):

class SocialAccountAdapter(DefaultSocialAccountAdapter):
    def authentication_error(self, request, provider_id, error, exception, extra_context):
        your_log_function(
            'SocialAccount authentication error!',
            'error',
            request,
            extra_data = {'provider_id': provider_id, 'error': error.__str__(), 'exception': exception.__str__(), 'extra_context': extra_context},
        )

(and in settings.py) (和settings.py中)

SOCIALACCOUNT_ADAPTER = "main.wherever.SocialAccountAdapter"

I'm doing this in my app and it works great! 我在我的应用程序中这样做,它很棒!

In my case I had everything ok except the flag SESSION_COOKIE_DOMAIN = mydomain.com which I only had to comment because I was working on localhost and not in mydomain.com.在我的例子中,除了SESSION_COOKIE_DOMAIN = mydomain.com标志外,我一切正常,我只需要发表评论,因为我在本地主机上工作,而不是在 mydomain.com 中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM