title: SSLCertificateError - "The handshake operation timed out" when trying to get an access token from facebook
body: <p>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.</p>
  
  <p>this is the code i am using</p>
  
  <pre><code>     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)
  </code></pre>
  
  <p>fails with this error:</p>
  
  <pre><code>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&amp;code=999&amp;client_id=229985173769038&amp;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
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>changed the code as @BluesRockAddict suggested, still getting the error</p>
  
  <p><strong>Edit #2</strong></p>
  
  <p>solved it! turned out to be a number of random different things.</p>
  
  <ul>
  <li>add the parameters to the url as required by a GET request.  (payload is only used in POST requests) as suggested by BlueRockAddict</li>
  <li>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</li>
  <li>urlfetch in app-engine does not return a 3-tuple like i thought. so i assigned the method result to a single variable</li>
  </ul>
  
  <p>here is the code that works for me:</p>
  
  <pre><code>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
  </code></pre>