我正在按照这个教程使用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(s):
https://127.0.0.1:5000/login/authorized
github.py (稍微修改的版本 https://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'))