Move facebook token to own script

To a PHP script actually. Not sure why PHP. Keeps morss' code cleaner. This piece of code had nothing to do in there, and didn't bring any advantage.
master
pictuga 2014-11-19 20:09:27 +01:00
parent a9654ea578
commit 105ca67744
2 changed files with 28 additions and 40 deletions

View File

@ -46,10 +46,6 @@ MIMETYPE = {
'xml': ['text/xml', 'application/xml', 'application/rss+xml', 'application/rdf+xml', 'application/atom+xml'], 'xml': ['text/xml', 'application/xml', 'application/rss+xml', 'application/rdf+xml', 'application/atom+xml'],
'html': ['text/html', 'application/xhtml+xml', 'application/xml']} 'html': ['text/html', 'application/xhtml+xml', 'application/xml']}
FBAPPID = "<insert yours>"
FBSECRET = "<insert yours>"
FBAPPTOKEN = FBAPPID + '|' + FBSECRET
PROTOCOL = ['http', 'https', 'ftp'] PROTOCOL = ['http', 'https', 'ftp']
if 'SCRIPT_NAME' in os.environ: if 'SCRIPT_NAME' in os.environ:
@ -673,7 +669,7 @@ def cgi_app(environ, start_response):
if 'HTTP_IF_NONE_MATCH' in environ: if 'HTTP_IF_NONE_MATCH' in environ:
options['last'] = int(environ['HTTP_IF_NONE_MATCH'][1:-1]) options['last'] = int(environ['HTTP_IF_NONE_MATCH'][1:-1])
if not options.force and not options.facebook and time.time() - options.last < DELAY: if not options.force and time.time() - options.last < DELAY:
headers['status'] = '304 Not Modified' headers['status'] = '304 Not Modified'
start_response(headers['status'], headers.items()) start_response(headers['status'], headers.items())
log(url) log(url)
@ -703,11 +699,6 @@ def cgi_app(environ, start_response):
url, cache = Init(url, os.getcwd() + '/cache', options) url, cache = Init(url, os.getcwd() + '/cache', options)
if options.facebook:
do_facebook(url, environ, headers, options, cache)
start_response(headers['status'], headers.items())
return
# get the work done # get the work done
rss = Fetch(url, cache, options) rss = Fetch(url, cache, options)
@ -786,36 +777,6 @@ def cli_app():
log('done') log('done')
def do_facebook(url, environ, headers, options, cache):
log('fb stuff')
query = urlparse.urlparse(url).query
if 'code' in query:
# get real token from code
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=environ['SCRIPT_URI'])
token = urlparse.parse_qs(urllib2.urlopen(eurl).read().strip())['access_token'][0]
# get long-lived access token
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=FBAPPID, app_secret=FBSECRET, short_lived_token=token)
values = urlparse.parse_qs(urllib2.urlopen(eurl).read().strip())
ltoken = values['access_token'][0]
expires = int(time.time() + int(values['expires'][0]))
headers['set-cookie'] = 'token={token}; Path=/'.format(token=ltoken)
# headers
headers['status'] = '303 See Other'
headers['location'] = 'http://{domain}/'.format(domain=environ['SERVER_NAME'])
log('fb done')
return
def main(): def main():
if 'REQUEST_URI' in os.environ: if 'REQUEST_URI' in os.environ:
wsgiref.handlers.CGIHandler().run(cgi_wrapper) wsgiref.handlers.CGIHandler().run(cgi_wrapper)

27
www/facebook.php 100644
View File

@ -0,0 +1,27 @@
<?php
define('FBAPPID', "<insert yours>");
define('FBSECRET', "<insert yours>");
define('FBAPPTOKEN', FBAPPID . '|' . FBSECRET);
if (isset($_GET['code']))
{
# get real token from code
$code = $_GET['code'];
$eurl = sprintf("https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s",
FBAPPID, $_SERVER['SCRIPT_URI'], FBSECRET, $code);
parse_str(file_get_contents($eurl), $values);
$token = $values['access_token'];
# get long-lived access token
$eurl = sprintf("https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s",
FBAPPID, FBSECRET, $token);
parse_str(file_get_contents($eurl), $values);
$ltoken = $values['access_token'];
setcookie('token', $ltoken, 0, '/');
# headers
header('status: 303 See Other');
header('location: http://' . $_SERVER['SERVER_NAME'] . '/');
}