its my first time working with keycloak.
im creating a react native app for my website which is auth each user with keycloak.
im able to login and get a response which is {"authentication": null, "error": null, "errorCode": null, "params": {"code": "32c1d719-c8ce-4c1b-8800-5a853095da71.3fe7ff3e-feb2-47c9-b101-b24c90d596b4.918d5daa-5b42-4480-8832-3c88c5e876ad", "session_state": "3fe7ff3e-feb2-47c9-b101-b24c90d596b4", "state": "sVF3SdDAYg"}, "type": "success", "url": "exp://10.11.1.10:8081?state=sVF3SdDAYg&session_state=3fe7ff3e-feb2-47c9-b101-b24c90d596b4&code=32c1d719-c8ce-4c1b-8800-5a853095da71.3fe7ff3e-feb2-47c9-b101-b24c90d596b4.918d5daa-5b42-4480-8832-3c88c5e876ad"}
这是我第一次使用钥匙斗篷。我正在为我的网站创建一个react原生应用程序,该应用程序使用keycloft对每个用户进行身份验证。我能够登录并获得一个响应,该响应为{“authentication”:null,“error”:null、“errorCode”:null和“params”:{“code”:“32c1d719-c8ce-4c1b-8800-5a853095da71.3f7ff3e-eb2-47c9-b101-b24c90d596b4.918d5daa-5b42-4480-8832-3c88c5e876ad”、“session_state”:“3f7ff 3e-eb2-4 7c9-b11-b24c90d 596b4”、“state”:“sVF3SdDAYg”}、“type”:“success”、“url“:”exp://10.11.1.10:8081?state=sVF3SdDAYgsession_state=3f7ff3e-eb2-47c9-b101-b24c90d596b4代码=32c1d719-c8ce-4c1b-8800-5a853095da71.3f7ff 3e-eb2-4 7c9-b11-b24c90d 596b4.918d5daa-5b42-4480-8832-3c88c5e876ad“}
but how do i get the user token to use for rest api's. my code to login is here :
但是我如何获得用于rest api的用户令牌呢。我的登录代码如下:
import React, { useEffect, useState } from 'react';
import * as WebBrowser from 'expo-web-browser';
import { useAuthRequest, useAutoDiscovery } from 'expo-auth-session';
import { Button, Text, View } from 'react-native';
WebBrowser.maybeCompleteAuthSession();
export default function LoginPage() {
const discovery = useAutoDiscovery('https://@@@@@@@@@@@@@@@@@@@@@@@@@');
const redirectUri = 'exp://10.11.1.10:8081';
const [accessToken, setAccessToken] = useState();
const [request, result, promptAsync] = useAuthRequest(
{
clientId: 'mywebsite',
redirectUri: redirectUri,
scopes: ['openid', 'profile'],
usePKCE: true,
},
discovery,);
console.log(result)
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button title="Login!" disabled={!request} onPress={() => promptAsync()} />
{result && <Text>{JSON.stringify(result, null, 2)}</Text>}
</View>
);
}
i tried to use token restapi endpoint with secret key but i get error no token..
我试图使用带有密钥的令牌restapi端点,但我得到错误no令牌。。
更多回答
优秀答案推荐
We can get an access token from keycloak using the /token endpoint if you're getting an error while hitting this api then check the body parameters we need to send the client_id,client_secret,grant_type,username and password.
我们可以使用/token端点从keycapture获取访问令牌。如果您在访问此api时遇到错误,请检查发送client_id、client_secret、grant_type、用户名和密码所需的主体参数。
I have exactly the same problem Tomas.
When you use pkce, you send a codeChallenge to Keycloak on login and you receive authorization code that you need to send back with the codeVerifier (the original code transformed into a codeChallenge).
托马斯,我也有同样的问题。当你使用pkce时,你会在登录时向Keycloft发送一个codeChallenge,并收到需要与codeVerifier一起发送回的授权代码(原始代码转换为codeChallenger)。
The problem is... I don't know how to make that !
问题是…我不知道怎么做!
edit :
I found something :
Authsession has a fonction : AuthSession.exchangeCodeAsync(config, discovery)
edit:我发现了一些东西:Authsession有一个函数:Authsession.exchangeCodeAsync(config,discovery)
I found a code example : https://gist.github.com/lemmensaxel/72ece5cd00026cc05888701d7d65fbe0
我发现了一个代码示例:https://gist.github.com/lemmensaxel/72ece5cd00026cc05888701d7d65fbe0
where this function is called after you receive this json then it send back the code verifier, and you get a access token :)
在这里,这个函数是在您收到这个json后调用的,然后它会发回代码验证器,您会得到一个访问令牌:)
I'm trying to do it
我正在努力
I have a success thanks to this man and his code !
多亏了这个人和他的密码,我取得了成功!
https://gist.github.com/lemmensaxel/72ece5cd00026cc05888701d7d65fbe0
https://gist.github.com/lemmensaxel/72ece5cd00026cc05888701d7d65fbe0
this is the only github that clearly explains how to set up authentication with keycloak in RN...
这是唯一一个清楚地解释了如何在RN中使用密钥斗篷设置身份验证的github。。。
I have a last error with android that I need to fix but it's ok !
我有最后一个错误与安卓,我需要修复,但它是可以的!
cheers
干杯
更多回答
Is this the answer?
这就是答案吗?
yes in typescript but you can use it with RN
是的,但你可以和RN一起使用
我是一名优秀的程序员,十分优秀!