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 に答える