Last active
February 8, 2018 16:27
-
-
Save bahorn/b3fd102cca078b31a55e7b75d984b1fe to your computer and use it in GitHub Desktop.
Hacked up Flask server to login to authorise as a facebook user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import binascii | |
import requests | |
from flask import Flask, request | |
app = Flask(__name__) | |
# Web app to get use a user token from Facebook | |
app_id = "<BLANKED>" | |
app_secret = "<BLANKED>" | |
redirect_uri = "<BLANKED>" | |
def login_uri(client_id, state, version="v2.12"): | |
base = "https://www.facebook.com/{}/dialog/oauth".format(version) | |
scope = ",".join(['manage_pages','publish_pages']) | |
return "{}?client_id={}&redirect_uri={}&state={}&scope={}".format( | |
base, client_id, redirect_uri, state, scope) | |
def get_api_token(code): | |
params = { | |
"client_id":app_id, | |
"client_secret":app_secret, | |
"redirect_uri":redirect_uri, | |
"code":code | |
} | |
print("Yeehah") | |
url = "https://graph.facebook.com/v2.12/oauth/access_token" | |
r = requests.get(url, params=params) | |
return r.json() | |
page = """ | |
<html> | |
<body> | |
<h1>Facebook authorize</h1> | |
<p> | |
<a href="{}">Click here to authorize this app</a> | |
</p> | |
</body> | |
</html> | |
""" | |
@app.route("/") | |
def index_page(): | |
state = binascii.b2a_hex(os.urandom(32)) | |
return page.format(login_uri(app_id,state)) | |
@app.route("/auth") | |
def auth_page(): | |
try: | |
code = request.args.get("code") | |
print(get_api_token(code)) | |
return json.dumps({"status":"ok"}) | |
except: | |
return json.dumps({"status":"failure"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment