Facebook Graph API からコンテンツを取得するための単純な Python Web サービスを作成しています。テスト中に、すべてを 1 つの python クラスにまとめて書いたところ、問題なく動作していました。モジュール化を開始した今、すべてがバラバラになりました。
RESTful サービスに webpy を使用しています。そして、私のコードは次のようになります。
webapp.py
import web
import FBModule
fb = FBModule.FBManager()
urls = (
'/', 'HomeHandler',
'/auth/login', 'LoginHandler',
'/auth/logout', 'LogoutHandler'
)
class BaseHandler():
def current_user(self):
if not hasattr(self, "_current_user"):
self._current_user = dict()
user_id = web.cookies().get('fb_userid')
user_name = web.cookies().get('fb_username')
if user_id and user_name:
self._current_user = {'id': user_id, 'name': user_name}
return self._current_user
class HomeHandler(BaseHandler):
def GET(self):
return render.oauth(current_user=self.current_user())
class LoginHandler:
def GET(self):
fb.login()
return web.seeother('/')
class LogoutHandler():
def GET(self):
fb.logout()
web.seeother('/')
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
FBModule.py
import urllib
import urlparse
import time
import web
FACEBOOK_APP_ID = "<APP ID>"
FACEBOOK_APP_SECRET = "<APP SECRET>"
def path_url():
return "http://localhost:8080" + web.ctx.fullpath
class FBManager:
def login(self):
data = web.input(code=None)
args = dict(client_id=FACEBOOK_APP_ID, redirect_uri=path_url())
if data.code is None:
return web.seeother('http://www.facebook.com/dialog/oauth?' + urllib.urlencode(args) + '&scope=' + urllib.quote('user_likes,friends_likes,user_birthday,friends_birthday'))
args['code'] = data.code
args['client_secret'] = FACEBOOK_APP_SECRET
response = urlparse.parse_qs(urllib.urlopen("https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args)).read())
access_token = response["access_token"][-1]
profile = json.load(urllib.urlopen("https://graph.facebook.com/me?fields=id,name,birthday,location,gender&" + urllib.urlencode(dict(access_token=access_token))))
web.setcookie('fb_userid', str(profile['id']), expires=time.time() + 7 * 86400)
web.setcookie('fb_username', str(profile['name']), expires=time.time() + 7 * 86400)
def logout(self):
web.setcookie('fb_userid', '', expires=time.time() - 86400)
web.setcookie('fb_username', '', expires=time.time() - 86400)
問題は、別のモジュールを使用して Facebook からアクセス トークンを取得すると、「破損したコンテンツ エラー」が発生することです。しかし、FBModule の login() 関数から LoginHandler クラスの GET() 関数まですべてをカット アンド ペーストするだけの簡単なことをすると、すべてがまたもや大変なことになります。ここで何が間違っているのかを見つけようとして、私は機知に富んでいます。
私が間違っていることを知っているかどうか教えてください。
レッツ、パリトッシュ