Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSLCertificateError - "The handshake operation timed out" when trying to get an access token from facebook

I am building an application which authenticates the user with javascript, and then reads the facebook signed_request info from the cookie in order to obtain an access token for use in the server. I keep on hitting a timeout error in the ssl handshake.

this is the code i am using

     logger.debug("authenticate with %s" % facebook_id)
     payload = {'client_id': settings.FACEBOOK_APP_ID,
                'client_secret': settings.FACEBOOK_APP_SECRET,
                'code': code ,
                'redirect_uri': settings.FACEBOOK_DEFAULT_REDIRECT_URI}
     url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(payload)
     access_token, status, headers = urlfetch.fetch(
                                        url=url,
                                        deadline=30,
                                        validate_certificate=False
                                     )
     logger.debug("recieved access token from fb %s" % access_token)

fails with this error:

DEBUG    2012-05-09 21:27:33,956 backends.py:24] authenticate with 546941722
DEBUG    2012-05-09 21:27:33,956 urlfetch_stub.py:317] Making HTTP request: host = graph.facebook.com, url = https://graph.facebook.com/oauth/access_token?client_secret=999&code=999&client_id=229985173769038&redirect_uri=http%3A%2F%2Fdev.bitesizedbeautyapp.appspot.com%2F, payload = , headers = {'Host': 'graph.facebook.com', 'Accept-Encoding': 'gzip', 'User-Agent': 'AppEngine-Google; (+http://code.google.com/appengine)'}
ERROR    2012-05-09 21:28:04,130 __init__.py:63] Exception in request:
Traceback (most recent call last):
  File "/work/glow/bitesizedbeauty/django/core/handlers/base.py", line 89, in get_response
    response = middleware_method(request)
  File "/work/glow/bitesizedbeauty/bitesizedbeautyapp/facebook_glue/middleware.py", line 27, in process_request
    user = authenticate(facebook_id = fb_uid, code = code)
  File "/work/glow/bitesizedbeauty/django/contrib/auth/__init__.py", line 55, in authenticate
    user = backend.authenticate(**credentials)
  File "/work/glow/bitesizedbeauty/bitesizedbeautyapp/facebook_glue/backends.py", line 33, in authenticate
    validate_certificate=False
  File "/usr/local/google_appengine/google/appengine/api/urlfetch.py", line 263, in fetch
    return rpc.get_result()
  File "/usr/local/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
    return self.__get_result_hook(self)
  File "/usr/local/google_appengine/google/appengine/api/urlfetch.py", line 374, in _get_fetch_result
    raise SSLCertificateError(str(err))
SSLCertificateError: ApplicationError: 6 _ssl.c:488: The handshake operation timed out

Edit

changed the code as @BluesRockAddict suggested, still getting the error

Edit #2

solved it! turned out to be a number of random different things.

  • add the parameters to the url as required by a GET request. (payload is only used in POST requests) as suggested by BlueRockAddict
  • i am using the client SDK to authenticate the user, and reading the code element from the fbsr_ cookie. In this case the redirect_uri should be empty
  • urlfetch in app-engine does not return a 3-tuple like i thought. so i assigned the method result to a single variable

here is the code that works for me:

def get_auth_token(code=None):
    if not code:
        return Null
    try:
        payload = {'client_id': settings.FACEBOOK_APP_ID,
                   'client_secret': settings.FACEBOOK_APP_SECRET,
                   'code': code,
                   'redirect_uri': ''}

        url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(payload)
        logging.debug("url going to be called = (%s)" % url)
        result = urlfetch.fetch(
            url=url,
            deadline=10,
            validate_certificate=False
        )
        access_token = result.content
        logging.info("recieved access_token from fb %s" % access_token)
    except SSLCertificateError as error:
        logging.error("SSLCertificateError when accessing oauth/access_token, error = %s " % error)
        return None
    except Exception as error2:
        logging.error("Other Error, error = %s " % error2)
        return None
like image 244
Nitzan Volman Avatar asked Sep 03 '25 02:09

Nitzan Volman


1 Answers

payload argument should only be used for POST/PUT requests. Since you're using GET, your payload data needs to be included in the URL. Try the following:

access_token, status, headers = urlfetch.fetch(
   "https://graph.facebook.com/oauth/access_token?" + 
   urllib.urlencode(payload))
like image 199
BluesRockAddict Avatar answered Sep 04 '25 14:09

BluesRockAddict