python - SSLCertificateError - 尝试从 facebook 获取访问 token 时出现 "The handshake operation timed out"

标签 python django facebook google-app-engine

我正在构建一个应用程序,它使用 JavaScript 对用户进行身份验证,然后从 cookie 中读取 facebooksigned_request 信息,以获得在服务器中使用的访问 token 。 我在 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 应该为空
  • 应用程序引擎中的 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

最佳答案

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

access_token, status, headers = urlfetch.fetch(
   "https://graph.facebook.com/oauth/access_token?" + 
   urllib.urlencode(payload))

关于python - SSLCertificateError - 尝试从 facebook 获取访问 token 时出现 "The handshake operation timed out",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10523906/

相关文章:

python - Pandas DataFrame 中字符串的条件编辑

python - 获取跨列的值计数-Pandas DataFrame

python - 使用 BeautifulSoup 从 investing.com 抓取 BTC/ETH 数据

python - Matplotlib。将子图与 Bbox 对齐

Javascript 在两次 HTML 注入(inject)后停止工作

python - 这个网址有什么问题?

使用属性进行 Django-haystack 结果过滤?

php - 带有长生命周期 token 的 Facebook PHP/JS SDK "Session has expired at unix time"

javascript - css 样式化 facebook 登录按钮,最佳实践

iphone - 使用 SocialFramework 登录 Facebook