Skip to content
This repository

URLError at /social-auth/complete/facebook/ #315

Closed
night-crawler opened this Issue · 9 comments

3 participants

Igor Matías Aguirre swimorsink
Igor

Hi. I have some trouble with facebook backend. Also i've tried @krvss fork and got the same error. I see, almost all backends work fine (google-oauth2, instagram, flickr, vkontakte, yandex, ...), so it seems the reason is on my side.
I guess the wrong settings may be the reason. I fill only "App Domain" and "Site URL" - is it enough?
So, i got the fb question after redirect, and then this:

Environment:

Request Method: GET
Request URL: http://mydomain.lol/social-auth/complete/facebook/?code=AQBOGXjtD2ruIyF3SHGKN_vpU9KszD1By52QjXHym7SU12_fMy1hfFJuRYDzqVfbVGIaYgSt9Lh5TfADqsBvqL5tzL3IJ6tT6i9b3G_V-vc0EJPzwDF9fEMG4LNTVEuDjq7Lp1Bo6wEFWxqy5vzMRuSr4mrlCoicS2xJtN27A1kpZSOR0FjBSTHnoLsBp28f6Hc

Django Version: 1.4
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'social_auth',
'tel.po',
'tel.telehome')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')

Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response

  1. response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  2. return view_func(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django_social_auth-0.6.7-py2.7.egg/social_auth/views.py" in wrapper
  3. return func(request, backend, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django_social_auth-0.6.7-py2.7.egg/social_auth/views.py" in complete
  4. return complete_process(request, backend, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django_social_auth-0.6.7-py2.7.egg/social_auth/views.py" in complete_process
  5. user = auth_complete(request, backend, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django_social_auth-0.6.7-py2.7.egg/social_auth/views.py" in auth_complete
  6. **kwargs) File "/usr/local/lib/python2.7/dist-packages/django_social_auth-0.6.7-py2.7.egg/social_auth/backends/facebook.py" in auth_complete
  7. response = cgi.parse_qs(urlopen(url).read()) File "/usr/lib/python2.7/urllib2.py" in urlopen
  8. return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py" in open
  9. response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py" in _open
  10. '_open', req) File "/usr/lib/python2.7/urllib2.py" in _call_chain
  11. result = func(*args) File "/usr/lib/python2.7/urllib2.py" in https_open
  12. return self.do_open(httplib.HTTPSConnection, req) File "/usr/lib/python2.7/urllib2.py" in do_open
  13. raise URLError(err)

Exception Type: URLError at /social-auth/complete/facebook/
Exception Value:

Matías Aguirre
Owner
omab commented

Seems that you are being disconnected when DSA exchanges the oauth token with an access_token. Maybe your domain mydomain.lol is the issue?

App domain and Site URL is enough.

Matías Aguirre
Owner
omab commented

Just tested with myapp.lol and it went well here. Another thing to check is the "Advanced" tab in facebook app configuration, check that App Type is Web, and also Sandbox Mode should be disabled (it causes some troubles that I need to check AFAIK).

Igor

mydomain.lol - is a subst for a real domain that works fine. App type is Web and Sandbox disabled. Other settings in django-social-auth are default. Also, it may be useful, it waits an answer for ~1min, and the next i see - this err message.

swimorsink

I spent a long time trying to figure this one out - the url that the facebook.py code is trying to fetch there on line 100 is
https://graph.facebook.com/oauth/access_token?code=

Basically, it's the last step that the library tries to carry out in order to request a long term access token. When I printed that url out and went to it in my browser, it worked fine. The problem appears to be related to urllib2 and certain https websites.
See http://stackoverflow.com/questions/9835506/urllib-urlopen-works-on-sslv3-urls-with-python-2-6-6-on-1-machine-but-not-wit

We found that the problem exists with python 2.7.3 but not with python 2.7.1, so our ultimate solution was to throw up our hands and revert to 2.7.1. Not a very good solution - we should probably submit a patch to set up urllib2 correctly in facebook.py

swimorsink

Oh, I should also add - I see your python version is 2.7.2 above. Are you using the python from MacPorts? We found that python 2.7.2 revision 1 worked fine, but revision 4 did not, and I believe the version of openssl changed somewhere in between there, which was the ultimate cause of the problem.

swimorsink

FYI, I found a workaround for this issue. It works using the latest python version on mac - 2.7.3. At the top of my login handler (NOT facebook.py), before I load my login page - I run the following code taken from Ian Weatherbee on http://bugs.python.org/issue11220.

custom HTTPS opener, banner's oracle 10g server supports SSLv3 only

import httplib, ssl, urllib2, socket
class HTTPSConnectionV3(httplib.HTTPSConnection):
def init(self, *args, **kwargs):
httplib.HTTPSConnection.init(self, *args, **kwargs)

def connect(self):
    sock = socket.create_connection((self.host, self.port), self.timeout)
    if self._tunnel_host:
        self.sock = sock
        self._tunnel()
    try:
        self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv3)
    except ssl.SSLError, e:
        print("Trying SSLv3.")
        self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv23)

class HTTPSHandlerV3(urllib2.HTTPSHandler):
def https_open(self, req):
return self.do_open(HTTPSConnectionV3, req)

install opener

urllib2.install_opener(urllib2.build_opener(HTTPSHandlerV3()))

This ensures that the sslv3 handler is installed before any other urllib2 code runs, which for me, fixes the issue coming from facebook.py.

Igor

@salomo851, i put that code into the top of my views, and it works fine! Thank you!
Here is the same code for easy copy-paste:

import httplib, ssl, urllib2, socket
class HTTPSConnectionV3(httplib.HTTPSConnection):
    def __init__(self, *args, **kwargs):
        httplib.HTTPSConnection.__init__(self, *args, **kwargs)

    def connect(self):
        sock = socket.create_connection((self.host, self.port), self.timeout)
        if self._tunnel_host:
            self.sock = sock
            self._tunnel()
        try:
            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv3)
        except ssl.SSLError, e:
            print("Trying SSLv3.")
            self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_SSLv23)

class HTTPSHandlerV3(urllib2.HTTPSHandler):
    def https_open(self, req):
        return self.do_open(HTTPSConnectionV3, req)

urllib2.install_opener(urllib2.build_opener(HTTPSHandlerV3()))

Are you using the python from MacPorts?

I use debian wheezy with python from repos, version is 2.7.2-8

Thanks again! I'll check if any other auth-backends work fine with this workaround.

Igor

vkontakte, twitter, mailru, google-oauth2, github, dropbox, linkedin, yandex, yahoo, instagram, flickr, foursquare works fine (i see, this check was not necessary, but %s). And also with fb-backend i got regirect back to /profile/#_=_, but it should be /profile/. It is not critical for me, but..

Matías Aguirre omab referenced this issue from a commit
Matías Aguirre Update misc section. Refs #315 47196b8
Matías Aguirre
Owner
omab commented

I've added a comment about this issue in the docs http://django-social-auth.readthedocs.org/en/latest/miscellaneous.html#python-2-7-2rev4-2-7-3-and-facebook-backend. Thanks for the bug hunting and the detailed explanation.

Matías Aguirre omab closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.