Doc*_*Doc 5 openid client keycloak flutter
我正在尝试通过openid_client验证我的 flutter 应用程序到keycloak
按照 repo 示例,我编写了这样的身份验证函数
authenticate() async {
// parameters here just for the sake of the question
var uri = Uri.parse('https://keycloak-url/auth/realms/myrealm');
var clientId = 'my_client_id';
var scopes = List<String>.of(['openid', 'profile']);
var port = 4200;
var redirectUri = Uri.parse('http://localhost:4200');
var issuer = await Issuer.discover(uri);
var client = new Client(issuer, clientId);
urlLauncher(String url) async {
if (await canLaunch(url)) {
await launch(url, forceWebView: true);
} else {
throw 'Could not launch $url';
}
}
var authenticator = new Authenticator(client,
scopes: scopes,
port: port,
urlLancher: urlLauncher,
redirectUri: redirectUri);
var c = await authenticator.authorize();
closeWebView();
var token= await c.getTokenResponse();
print(token);
return token;
}
Run Code Online (Sandbox Code Playgroud)
当我调用该函数时,会出现一个 webview 弹出窗口,我可以通过 keycloak 登录,但是当弹出窗口关闭时,我在以下位置收到此错误c.getTokenResponse():
发生异常。NoSuchMethodError (NoSuchMethodError: The getter 'length' was called on null.Receiver: null Tried call: length)
检查 Credential c,我可以看到 TokenResponse 只有“状态”、“会话状态”和“代码”字段
我错过了什么?
我已经在 github 上得到了回答(链接),所以我将在这里复制解决方案:
在移动设备上,您应该使用 PKCE 流程。当您在 Authenticator 构造函数中省略重定向 uri 时,会自动选择此选项。
所以,应该是:
var authenticator = new Authenticator(client,
scopes: scopes,
port: port,
urlLancher: urlLauncher,);
Run Code Online (Sandbox Code Playgroud)
确保将 uri http://localhost:4200/(包括尾部斜杠)添加到Valid Redirect URIskeycloak 中。

确保将 uri http://localhost:4200/(包括尾部斜杠)添加到 keycloak 中的有效重定向 URI。
| 归档时间: |
|
| 查看次数: |
7068 次 |
| 最近记录: |