gpt4 book ai didi

python - Flask-OAuthlib OAuth2 客户端报错: "Missing access credentials."

转载 作者:行者123 更新时间:2023-11-28 19:19:10 25 4
gpt4 key购买 nike

我正在按照本教程使用 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.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'))

最佳答案

你也需要定义一个范围:例如:

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',
]

关于python - Flask-OAuthlib OAuth2 客户端报错: "Missing access credentials.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com