【问题标题】:How to read all approved items from Facebook with Flask如何使用 Flask 从 Facebook 读取所有已批准的项目
【发布时间】:2015-10-16 22:31:00
【问题描述】:

给定FACEBOOK_APP_IDFACEBOOK_APP_SECRET,我在下面的代码中(甚至在FB控制面板上)更改什么声音以便能够读取emailpublic_profileuser_friends用户(我)?

from flask import Flask, redirect, url_for, session, request
from flask_oauth import OAuth

SECRET_KEY = ''
DEBUG = True
FACEBOOK_APP_ID = ''
FACEBOOK_APP_SECRET = ''

app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()

facebook = oauth.remote_app('facebook',
    base_url='https://graph.facebook.com/',
    request_token_url=None,
    access_token_url='/oauth/access_token',
    authorize_url='https://www.facebook.com/dialog/oauth',
    consumer_key=FACEBOOK_APP_ID,
    consumer_secret=FACEBOOK_APP_SECRET,
    request_token_params={'scope': ["email", "public_profile", "user_friends"]}
)

@app.route('/')
def index():
    return redirect(url_for('login'))

@app.route('/login')
def login():
    return facebook.authorize(callback=url_for('facebook_authorized',
        next=request.args.get('next') or request.referrer or None,
        _external=True))

@app.route('/login/authorized')
@facebook.authorized_handler
def facebook_authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')
    return 'type %s, data %s, headers %s, raw_data %s, status %s' % (type(me), str(me.data), str(me.headers), str(me.raw_data), str(me.status))

@facebook.tokengetter
def get_facebook_oauth_token():
    return session.get('oauth_token')

if __name__ == '__main__':
    app.run()

编辑:

request_token_params={'scope': 'public_profile,user_friends,email'}

me = facebook.get('/me?fields=id,name,first_name,last_name,age_range,link,gender,locale,timezone,updated_time,verified,friends,email')

【问题讨论】:

    标签: python facebook oauth flask


    【解决方案1】:

    如果您使用的是 Graph API v2.4,则需要指定要返回的所有字段

    me = facebook.get('/me?fields=id,name,gender,email,friends')
    

    等等。这一切都在文档中。

    【讨论】:

    • 你需要更具体。什么不完全有效?此外,您仅将email 指定为范围参数,缺少public_profileuser_friends。如果您不请求权限,则无法获取后者的数据
    • 当然。我更新了scope。不确定这是否是您的意思。你能看看吗?此外,响应返回status200,据我所知facebook-api-versionv2.4。那么有什么问题呢?
    • 我的意思是实际问题是什么。不,我无法从您的代码中确定出了什么问题(如果有问题)。 Stackoverflow 没有调试服务。我们只能给你有效的提示......不过,你需要请求每个字段,正如我在回答中已经写的那样。我在您的代码中没有看到这一点。
    • 我将 request_token_params{'scope': "email"} 设置为 {'scope': ["email", "public_profile", "user_friends"]}request_token_params 是函数 oauth.remote_app 的参数。这不是你建议的吗?
    • 你只能得到那些也使用你的应用程序的朋友。可能你的朋友都没有使用你的应用程序......这是自 v2.0 以来的行为,意味着自 15 个月以来......见developers.facebook.com/docs/apps/changelog#v2_0 引用:The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app. 如果有帮助,请接受我的回答。
    猜你喜欢
    • 2020-01-06
    • 1970-01-01
    • 2019-02-01
    • 2023-03-21
    • 2021-11-22
    • 2017-12-16
    • 2015-11-12
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多