我正在尝试使用简单的Flask API设置密钥克隆集成。我正在尝试使用https://gist.github.com/thomasdarimont/145dc9aa857b831ff2eff221b79d179a作为设置简单api的参考。在堆栈溢出时,我曾在此问题上看到过类似的问题,但是我没有通过这些问题得到任何解决方案。也许有人可以帮助我解决这个问题。
我的代码:
app.py
import json
import logging
from flask import Flask, g
from flask_oidc import OpenIDConnect
import requests
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
app.config.update({
'SECRET_KEY': 'SomethingNotEntirelySecret',
'TESTING': True,
'DEBUG': True,
'OIDC_CLIENT_SECRETS': 'client_secrets.json',
'OIDC_ID_TOKEN_COOKIE_SECURE': False,
'OIDC_REQUIRE_VERIFIED_EMAIL': False,
'OIDC_USER_INFO_ENABLED': True,
'OIDC_OPENID_REALM': 'apiv3-login',
'OIDC_SCOPES': ['openid', 'email', 'profile'],
'OIDC_INTROSPECTION_AUTH_METHOD': 'client_secret_post',
'OIDC_TOKEN_TYPE_HINT': 'access_token'
})
oidc = OpenIDConnect(app)
@app.route('/')
def hello_world():
if oidc.user_loggedin:
return ('Hello, %s, <a href="/private">See private</a> '
'<a href="/logout">Log out</a>') % \
oidc.user_getfield('preferred_username')
else:
return 'Welcome anonymous, <a href="/private">Log in</a>'
@app.route('/api', methods=['POST'])
@oidc.accept_token(require_token=True)
def hello_api():
"""OAuth 2.0 protected API endpoint accessible via AccessToken"""
return json.dumps({'hello': 'Welcome %s' % g.oidc_token_info['sub']})
@app.route('/logout')
def logout():
"""Performs local logout by removing the session cookie."""
oidc.logout()
return 'Hi, you have been logged out! <a href="/">Return</a>'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
client_secrets.json
{
"web": {
"issuer": "http://localhost:8080/auth/realms/apiv3",
"auth_uri": "http://localhost:8080/auth/realms/apiv3/protocol/openid-connect/auth",
"client_id": "apiv3-login",
"client_secret": "5551fe75-38c5-435a-a392-a6da6252439e",
"redirect_uris": [
"http://localhost:5000/*"
],
"userinfo_uri": "http://localhost:8080/auth/realms/apiv3/protocol/openid-connect/userinfo",
"token_uri": "http://localhost:8080/auth/realms/apiv3/protocol/openid-connect/token",
"token_introspection_uri": "http://localhost:8080/auth/realms/apiv3/protocol/openid-connect/token/introspect"
}
}
我正在尝试使用端点(使用邮递员)获取令牌
http://localhost:8080/auth/realms/apiv3/protocol/openid-connect/token
,授予类型:密码。
使用访问令牌,我尝试从上述步骤中使用授权标头载体访问令牌调用localhost:5000 / api。我不确定自己在做什么错。
烧瓶错误显示ERROR:flask_oidc:ERROR: Unable to get token info