我编写了一个使用flask的rest api(python应用程序)。它会ping Keycloak以验证用户身份。无论传递给它什么参数,我都会收到无效的令牌响应(来自Keycloak)。我们是keycloak的新手。任何见解或帮助将真的很有帮助和赞赏。我们在这里几乎被封锁了。我们尝试了不同的参数组合,但始终获得相同的结果。
使用邮递员,从密钥斗篷发送回的响应是401,响应包含:
{“错误”:“ invalid_token”,“错误说明”:“需要令牌,但无效”}
示例python代码为:
from flask import Flask, g
from flask_oidc import OpenIDConnect
app = Flask(__name__)
app.config.update({
'SECRET_KEY': 'Testing connection',
'TESTING': True,
'DEBUG': True,
'OIDC_CLIENT_SECRETS': '../client-secrects.json',
'OIDC_OPENID_REALM': 'Leads-realm',
#'OIDC_ID_TOKEN_COOKIE_SECURE': False,
#'OIDC_REQUIRE_VERIFIED_EMAIL': False,
#'OIDC_USER_INFO_ENABLED': True,
#'OIDC_VALID_ISSUERS': ['http://10.1.71.100:8090/auth/realms/Leads-realm'],
'OIDC_USER_INFO_ENABLED': True,
'OIDC_SCOPES': ['openid', 'email', 'profile'],
'OIDC_INTROSPECTION_AUTH_METHOD': 'client_secret_post',
'OIDC_TOKEN_TYPE_HINT': 'access_token'
})
oidc = OpenIDConnect(app)
@app.route("/helloworld", methods=['POST'])
def hello():
return "Hello World"
@app.route('/tkc', methods=['POST'])
@oidc.accept_token(require_token=True)
def tkc():
return ('Hello %s' %g.oidc_token_info['sub'])
if __name__ == "__main__":
app.run(host='localhost', port=5000)
client-secrects.json文件为:
{
"web": {
"issuer": "http://10.1.71.100:8090/auth/realms/Leads-realm",
"auth_uri": "http://10.1.71.100:8090/auth/realms/Leads-realm/protocol/openid-connect/auth",
"client_id": "leads-portal",
"client_secret": "b135396f-1557-436d-9b5f-1278b04e4c2f",
"redirect_uris": [
"http://localhost:5000/*"
],
"userinfo_uri": "http://10.1.71.100:8090/auth/realms/Leads-realm/protocol/openid-connect/userinfo",
"token_uri": "http://10.1.71.100:8090/auth/realms/Leads-realm/protocol/openid-connect/token",
"token_introspection_uri": "http://10.1.71.100:8090/auth/realms/Leads-realm/protocol/openid-connect/token/introspect",
"ssl-required": "external",
"resource": "leads-portal"
}
}