3

我正在构建一个应用程序,它使用 javascript 对用户进行身份验证,然后从 cookie 中读取 facebook signed_request 信息,以获取用于服务器的访问令牌。我一直在 ssl 握手中遇到超时错误。

这是我正在使用的代码

     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)

失败并出现此错误:

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

编辑

按照@BluesRockAddict 的建议更改了代码,仍然出现错误

编辑#2

解决了!原来是一些随机的不同的东西。

  • 根据 GET 请求的要求将参数添加到 url。(有效载荷仅用于 POST 请求)正如 BlueRockAddict 所建议的那样
  • 我正在使用客户端 SDK 对用户进行身份验证,并从 fbsr_ cookie 中读取代码元素。在这种情况下,redirect_uri 应该为空
  • app-engine 中的 urlfetch 不会像我想的那样返回 3 元组。所以我将方法结果分配给单个变量

这是对我有用的代码:

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
4

1 回答 1

3

payload参数只能用于 POST/PUT 请求。由于您使用的是 GET,因此您的有效负载数据需要包含在 URL 中。尝试以下操作:

access_token, status, headers = urlfetch.fetch(
   "https://graph.facebook.com/oauth/access_token?" + 
   urllib.urlencode(payload))
于 2012-05-09T21:10:27.257 回答