4

我有以下处理程序

首先,用户调用此处理程序并被重定向到 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)

在他授权我的应用程序 facebook 重定向到重定向 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 + 过期了。当代码试图从图表(graph/me)中获取用户信息时,有时也会发生同样的事情。

4

1 回答 1

1

关键问题不是facebook。这是 AppEngine 部署过程。我总是实时测试代码的更改,而不是本地测试,因为 OAuth 无法正常工作。因此,该deployment -> flush casche -> flush database过程似乎有一定的延迟,导致工件保留,从而混淆了代码。

因此,如果您必须实时测试 OAuth 之类的东西,我建议将更改部署为应用程序的新版本,并在部署后删除所有可能在新版本中充当工件的数据。

于 2012-05-04T22:43:34.163 回答