通过Flask OAuth2处理来自Facebook的回调代码/检索令牌?

时间:2016-03-27 01:54:18

标签: python facebook facebook-graph-api oauth flask

我想将令牌作为字符串返回给另一个python脚本。但是我陷入了回调页面。我将redirect_uri改为" / callback"它将URL中的授权代码作为

返回
www.my.website.com/callback?code={code}. 

位于http://requests-oauthlib.readthedocs.org/en/latest/examples/facebook.html的Facebook OAuth 2示例。通过粘贴重定向URI,我一直收到404错误。

我还想请求以下参数:{id,message,created_time},但我不确定如何。

import os
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, render_template, url_for
from requests_oauthlib.compliance_fixes import facebook_compliance_fix

app = Flask(__name__)
app.secret_key = os.urandom(24)

@app.route("/login")
def demo():

    client_id = '1624399237811291'
    client_secret = 'SECRET'

    # OAuth endpoints given in the Facebook API documentation

    authorization_base_url = 'https://www.facebook.com/dialog/oauth'
    token_url = 'https://graph.facebook.com/oauth/access_token'
    redirect_uri = 'http://www.bbg.mybluemix.net/callback'     # Should match Site URL
    facebook = OAuth2Session(client_id, redirect_uri=redirect_uri)
    facebook = facebook_compliance_fix(facebook)

    # Redirect user to Facebook for authorization
    authorization_url, state = facebook.authorization_url(authorization_base_url)
    authurl = '%s' % authorization_url

    return render_template('welcome.html', authurl=authurl)

    # Get the authorization verifier code from the callback url

@app.route("/callback", methods=["GET"])
def callback():
# Fetch the access token

    redirect_response = ' '
    token = facebook.fetch_token(token_url, client_secret=client_secret,
                     authorization_response=redirect_response)
    return '%s' % token


# Fetch a protected resource, i.e. user profile
#r = facebook.get('https://graph.facebook.com/me?')
#return '%s' % r.content

0 个答案:

没有答案