Skip to content

Instantly share code, notes, and snippets.

@bahorn
Last active February 8, 2018 16:27
Show Gist options
  • Select an option

  • Save bahorn/b3fd102cca078b31a55e7b75d984b1fe to your computer and use it in GitHub Desktop.

Select an option

Save bahorn/b3fd102cca078b31a55e7b75d984b1fe to your computer and use it in GitHub Desktop.

Revisions

  1. B Horn renamed this gist Feb 8, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. B Horn created this gist Feb 8, 2018.
    56 changes: 56 additions & 0 deletions it.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    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"})