2012-05-04 11 views
4

I持って次のハンドラFacebookの「ログイン」(のOAuth2)

まず、ユーザは、このハンドラを呼び出すとFacebookにリダイレクトされます:彼は私のアプリのFacebookはにリダイレクト許可後

class LoginFacebookHandler(BasicHandler): 
    def get(self): 
     user = self.auth.get_user_by_session() 
     if not user: 
      h = hashlib.new('sha512') 
      h.update(str(datetime.now())+"abc") 
      nonce = h.hexdigest() 
      logging.info("hash "+str(nonce)) 
      memcache.set(str(nonce), True, 8600) 
      #facebook_uri = "https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&state=%s&scope=%s" % ("20773", "http://upstrackapp.appspot.com/f", str(nonce), "email") 
      data = {"client_id": 20773, "redirect_uri": "http://***.appspot.com/f", "state": str(nonce), "scope": "email"} 
      facebook_uri = "https://www.facebook.com/dialog/oauth?%s" % (urllib.urlencode(data)) 
      self.redirect(facebook_uri) 

URI(ハンドラ)リダイレクト:

class CreateUserFacebookHandler(BasicHandler): 
    def get(self): 
     state = self.request.get('state') 
     code = self.request.get('code') 
     logging.info("state "+state) 
     logging.info("code "+code) 
     if len(code) > 3 and len(state) > 3: 
      cached_state = memcache.get(str(state)) 
      logging.info("cached_state "+str(cached_state)) 
      if cached_state: 
       #memcache.delete(str(state)) 
       data = { "client_id": 20773, "redirect_uri": "http://***.appspot.com/f", "client_secret": "7f587", "code": str(code)} 
       graph_url = "https://graph.facebook.com/oauth/access_token?%s" % (urllib.urlencode(data)) 
       logging.info("grph url "+graph_url) 

       result = urlfetch.fetch(url=graph_url, method=urlfetch.GET) 
       if result.status_code == 200: 
        fb_response = urlparse.parse_qs(result.content) 
        access_token = fb_response["access_token"][0] 
        token_expires = fb_response["expires"][0] 
        logging.info("access token "+str(access_token)) 
        logging.info("token expires "+str(token_expires)) 
        if access_token: 
         api_data = { "access_token": str(access_token)} 
         api_url = "https://graph.facebook.com/me?%s" % (urllib.urlencode(api_data)) 
         logging.info("api url "+api_url) 
         api_result = urlfetch.fetch(url=api_url, method=urlfetch.GET) 
         if api_result.status_code == 200: 
          api_content = json.loads(api_result.content) 
          user_id = str(api_content["id"]) 
          email = str(api_content["email"]) 
          logging.info("user id "+str(user_id)) 
          logging.info("email "+str(email)) 
          h = hashlib.new('sha512') 
          h.update(str(user_id)+"abc") 
          password = h.hexdigest() 
          expire_data = datetime.now() + timedelta(seconds=int(token_expires)) 
          user = self.auth.store.user_model.create_user(email, password_raw=password, access_token=access_token, token_expires=expire_data, fb_id=user_id) 
         else: 
          self.response.write.out.write("error contacting the graph api") 
        else: 
         self.response.out.write("access token not long enough") 
       else: 
        self.response.out.write("error while contacting facebook server") 
      else: 
       self.response.out.write("error no cached state") 
     else: 
      self.response.out.write("error too short") 

コードがaccess_tokenはを取得しようと、私はなって終わるまでは主にこの作品「接触しているときにエラーが....」。 面白いのは、すべてのURL、状態などをログに記録するので、ログにコピーします。& urlfetchが開こうとしたURL(fb api-> access_token)をブラウザに貼り付けて、私のaccess_tokenを貼り付けます+は期限切れです。 同じことが、コードがグラフ(グラフ/私)からユーザー情報を取得しようとしたときに、同じことが起こることがあります。

+0

FBからどのようなエラーが発生しているかを確認するにはresult.contentをログに記録してください。 –

答えて

1

重要な問題はFacebookではありません。 AppEngineの展開プロセスです。 OAuthが正常に動作しないため、私は常にローカルではなくコードの変更をテストしました。 したがって、deployment -> flush casche -> flush databaseプロセスにはアーティファクトが残っている特定の遅延があり、コードを混乱させました。

あなたはライブのOAuthのようなものをテストする必要があれば、私は、アプリの新バージョンとして変更の配備をお勧めしたいと展開した後、新しいバージョンでの成果物として作用し得るすべてのデータを削除するものとします。

+1

FBログインを容易にするプロジェクトsimpleauthとengineauthをチェックしましたか?これらは非常に優れており、複数のOAuth 2.0プロバイダがwebapp2ユーザモデルのUserエンティティを保存できるようにしています。 –

+2

@NickRosencrantz非常にありがとうNick、はい私はそのようなライブラリをチェックしましたが、私はそれらのライブラリに慣れていないので、私はそれを私自身で実装したかったのです。 また、それらは基本的に私と同じことをします(webapp2_sessions + webapp2_auth、ユーザーモデルを拡張する、ndb tada) –