Fix Facebook support

Now token is grabbed directly by the server, and sent back by means of a cookie. This does unify token "creation" and renewal.
master
pictuga 2014-02-21 14:36:06 +01:00
parent a1f5c3db3a
commit c94ef92131
2 changed files with 22 additions and 47 deletions

View File

@ -83,42 +83,7 @@ def PreWorker(url, cache):
token = urlparse.parse_qs(urlparse.urlparse(url).query)['access_token'][0]
if token not in facebook['token']:
# this token ain't known, look for info about it
eurl = "https://graph.facebook.com/debug_token?input_token={token}&access_token={app_token}".format(token=token, app_token=morss.FBAPPTOKEN)
data = json.loads(urllib2.urlopen(eurl).read())['data']
app_id = str(data['app_id'])
user_id = str(data['user_id'])
expires = int(data['expires_at'])
short = 'issued_at' not in data
facebook['token'][token] = {'user': user_id, 'expires': expires}
# do some woodoo to know if we already have sth better
if user_id not in facebook['user']:
# grab a new one anyway, new user
facebook['user'][user_id] = {'original': token}
good = True
else:
# maybe it's a better one
last = facebook['user'][user_id]['token']
last_expires = facebook['token'][last]['expires']
if expires > last_expires:
# new is better
good = True
if good and short and app_id == morss.FBAPPID:
eurl = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={short_lived_token}".format(app_id=morss.FBAPPID, app_secret=morss.FBSECRET, short_lived_token=token)
values = urlparse.parse_qs(urllib2.urlopen(eurl).read().strip())
token = values['access_token'][0]
expires = int(time.time() + int(values['expires'][0]))
facebook['token'][token] = {'user': user_id, 'expires': expires}
facebook['user'][user_id]['token'] = token
return
# hey look for a newer token and use it
token = urlparse.parse_qs(urlparse.urlparse(url).query)['access_token'][0]

View File

@ -709,6 +709,12 @@ def cgi_app(environ, start_response):
headers['content-type'] = 'text/xml'
url, cache = Init(url, os.getcwd() + '/cache', options)
if options.facebook:
doFacebook(url, headers, options, cache)
start_response(headers['status'], headers.items())
return
RSS = Fetch(url, cache, options)
if headers['content-type'] == 'text/xml':
@ -757,12 +763,16 @@ def cli_app():
log('done')
if options.facebook:
facebook = Cache(cachePath, 'facebook', persistent=True, dic=True)
def doFacebook(url, headers, options, cache):
log('fb stuff')
facebook = cache.new('facebook', persistent=True, dic=True)
query = urlparse.urlparse(url).query
if 'code' in query:
# get real token from code
code = urlparse.parse_qs(urlparse.urlparse(url).query)['code'][0]
eurl = "https://graph.facebook.com/oauth/access_token?client_id={app_id}&redirect_uri={redirect_uri}&client_secret={app_secret}&code={code_parameter}".format(app_id=FBAPPID, app_secret=FBSECRET, code_parameter=code, redirect_uri="http://test.morss.it/:facebook/")
code = urlparse.parse_qs(query)['code'][0]
eurl = "https://graph.facebook.com/oauth/access_token?client_id={app_id}&redirect_uri={redirect_uri}&client_secret={app_secret}&code={code_parameter}".format(app_id=FBAPPID, app_secret=FBSECRET, code_parameter=code, redirect_uri="http://morss.it/:facebook/")
token = urlparse.parse_qs(urllib2.urlopen(eurl).read().strip())['access_token'][0]
# get long-lived access token
@ -773,7 +783,7 @@ def cli_app():
expires = int(time.time() + int(values['expires'][0]))
# get user id
iurl = "https://graph.facebook.com/me?fields=id&access_token={token}".format(ltoken)
iurl = "https://graph.facebook.com/me?fields=id&access_token={token}".format(token=ltoken)
user_id = json.loads(urllib2.urlopen(iurl).read())['id']
# do sth out of it
@ -785,14 +795,14 @@ def cli_app():
facebook.save()
if 'REQUEST_URI' in os.environ:
print 'Status: 200'
print 'Content-Type: text/plain'
print ''
headers['set-cookie'] = 'token={token}; Path=/'.format(token=ltoken)
print "token updated"
# headers
headers['status'] = '303 See Other'
headers['location'] = 'http://morss.it/'
sys.exit(0)
log('fb done')
return
def main():
if 'REQUEST_URI' in os.environ: