我不得不爲了我的註銷工作而程序化地過期cookie。從Facebook鏈接不工作,但在服務器端的OAuth可以做註銷無javascript:
class LogoutHandler(webapp2.RequestHandler):
def get(self):
self.set_cookie("fbsr_" + facebookconf.FACEBOOK_APP_ID, None, expires=time.time() - 86400)
self.redirect("/")
def set_cookie(self, name, value, expires=None):
if value is None:
value = 'deleted'
expires = datetime.timedelta(minutes=-50000)
jar = Cookie.SimpleCookie()
jar[name] = value
jar[name]['path'] = '/'
if expires:
if isinstance(expires, datetime.timedelta):
expires = datetime.datetime.now() + expires
if isinstance(expires, datetime.datetime):
expires = expires.strftime('%a, %d %b %Y %H:%M:%S')
jar[name]['expires'] = expires
self.response.headers.add_header(*jar.output().split(': ', 1))
你能更新你的問題告訴我們如何去,以及我們如何處理餅乾。對於Facebook宣誓,你是否註銷工作?我不得不添加一個cookie處理和一個註銷處理程序來爲我工作,但現在經過很多故障排除後,它可以工作。我登錄是這樣的:
class OAuthHandler(I18NHandler):
def get(self):
args = dict(
code = self.request.get('code'),
client_id = facebookconf.FACEBOOK_APP_ID,
client_secret = facebookconf.FACEBOOK_APP_SECRET,
redirect_uri = 'http://www.koolbusiness.com/oauth',
)
file = urllib.urlopen("https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args))
try:
token_response = file.read()
finally:
file.close()
access_token = cgi.parse_qs(token_response)["access_token"][-1]
graph = main.GraphAPI(access_token)
user = graph.get_object("me")
self.response.out.write(user["id"])
self.response.out.write(user["name"])
def get(self):
fbuser=None
profile = None
access_token = None
accessed_token = None
if self.request.get('code'):
args = dict(
code = self.request.get('code'),
client_id = facebookconf.FACEBOOK_APP_ID,
client_secret = facebookconf.FACEBOOK_APP_SECRET,
redirect_uri = 'http://'+self.get_host()+'/',
)
logging.debug("client_id"+str(args))
file = urllib.urlopen("https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(args))
try:
logging.debug("reading file")
token_response = file.read()
logging.debug("read file"+str(token_response))
finally:
file.close()
access_token = cgi.parse_qs(token_response)["access_token"][-1]
graph = main.GraphAPI(access_token)
user = graph.get_object("me") #write the access_token to the datastore
fbuser = main.FBUser.get_by_key_name(user["id"])
logging.debug("fbuser "+fbuser.name)
if not fbuser:
fbuser = main.FBUser(key_name=str(user["id"]),
id=str(user["id"]),
name=user["name"],
profile_url=user["link"],
access_token=access_token)
fbuser.put()
elif fbuser.access_token != access_token:
fbuser.access_token = access_token
fbuser.put()
current_user = main.get_user_from_cookie(self.request.cookies, facebookconf.FACEBOOK_APP_ID, facebookconf.FACEBOOK_APP_SECRET)
if current_user:
graph = main.GraphAPI(current_user["access_token"])
profile = graph.get_object("me")
accessed_token = current_user["access_token"]
謝謝肖恩,這解決了我的問題。如果「官方」python SDK可以處理這兩種情況(oauth,non-oauth),那肯定會很好。 – mainsocial
哇。救了我。 – Uri
我認爲獲得facebook.py文件的最好的地方是去官方的Facebook庫python這裏:https://github.com/facebook/python-sdk/network。因爲它已經過時了,所以你必須選擇一個最新的分支並使用它。來自Sean M的例子很糟糕,使用它你會最終連接每次驗證你的cookie,這已經被固定在例如'pythonforfacebook'fork中 –