4

我正在按照本教程使用 Flask OauthLib,尝试制作一个基本的 OAuth2 客户端以与 Foursquare.com 一起使用:https ://flask-oauthlib.readthedocs.org/en/latest/client.html#oauth2-client

在我授予使用该应用程序的权限后,我被重定向到包含以下文本的页面:

{
  "meta": {
    "code": 400, 
    "errorDetail": "Missing access credentials. See https://developer.foursquare.com/docs/oauth.html for details.", 
    "errorType": "invalid_auth"
  }, 
  "response": {}
}

怎么了?我该如何解决?谢谢。

Foursquare 应用程序设置:

重定向 URI: https://127.0.0.1:5000/login/authorized

github.pyhttps://github.com/lepture/flask-oauthlib/blob/master/example/github.py的略微修改版本)

from flask import Flask, redirect, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth


app = Flask(__name__)
app.debug = True
app.secret_key = 'development'

oauth = OAuth()
foursquare = oauth.remote_app(
    'foursquare',
    app_key='FOURSQUARE',
    consumer_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    consumer_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
)

app.config['FOURSQUARE'] = dict(
    consumer_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    consumer_secret='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    base_url='https://api.foursquare.com/',
    request_token_url=None,
    access_token_url='https://foursquare.com/oauth2/access_token',
    authorize_url='https://foursquare.com/oauth2/authenticate',
)
oauth.init_app(app)


@app.route('/')
def index():
    if 'foursquare_token' in session:
        me = foursquare.get('v2/users/self')
        return jsonify(me.data)
    return redirect(url_for('login'))


@app.route('/login')
def login():
    return foursquare.authorize(callback=url_for('authorized', _external=True))


@app.route('/logout')
def logout():
    session.pop('foursquare_token', None)
    return redirect(url_for('index'))


@app.route('/login/authorized')
def authorized():
    resp = foursquare.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error'],
            request.args['error_description']
        )
    session['foursquare_token'] = (resp['access_token'], '')
    me = foursquare.get('v2/users/self')
    return jsonify(me.data)


@foursquare.tokengetter
def get_foursquare_oauth_token():
    return session.get('foursquare_token')


if __name__ == '__main__':
    app.run('127.0.0.1', debug=True, port=5000, ssl_context=('/Users/XXXXX/Development/Certificates/server.crt', '/Users/XXXXX/Development/Certificates/server.key'))
4

1 回答 1

0

您还需要定义一个范围:例如:

request_token_params={
    'scope': [
         'https://mail.google.com/',
         'https://www.googleapis.com/auth/admin.directory.user.readonly',
         'https://www.googleapis.com/auth/admin.directory.orgunit.readonly',
         'https://www.googleapis.com/auth/admin.directory.group.readonly',
         'https://www.googleapis.com/auth/userinfo.email',
         'https://www.googleapis.com/auth/userinfo.profile',
         'https://www.googleapis.com/auth/plus.profile.emails.read',
]
于 2015-07-29T10:44:23.140 回答