gpt4 book ai didi

how do i get keycloak auth token in react native?(如何在Reaction本机中获取密钥罩身份验证令牌?)

转载 作者:bug小助手 更新时间:2023-10-27 19:55:45 25 4
gpt4 key购买 nike



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"}

这是我第一次使用钥匙斗篷。我正在为我的网站创建一个Reaction原生应用程序,这是用钥匙斗篷验证每个用户。我登录后得到的响应为:{“身份验证”:空,“错误”:空,“错误代码”:空,“参数”:{“代码”:“32c1d719-c8ce-4c1b-8800-5a853095da71.3fe7ff3e-feb2-47c9-b101-b24c90d596b4.918d5daa-5b42-4480-8832-3c88c5e876ad”,“会话_状态”:“3fe7ff3e-feb2-47c9-b101-b24c90d596b4”,“状态”:“sVF3SdDAYg”},“类型”:“成功”,“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”}


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终结点与密钥一起使用,但出现错误:无令牌。


更多回答
优秀答案推荐

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.

如果您在访问此API时遇到错误,我们可以使用/Token端点从keyloak获取访问令牌,然后检查我们需要发送的客户端id、客户端秘密、授权类型、用户名和密码的主体参数。



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时,您在登录时向Keyshaak发送一个codeChallenger,您将收到需要与codeVerier一起发回的授权码(原始代码被转换为codeChallest)。


The problem is... I don't know how to make that !

问题是..。我不知道怎么做!


edit :
I found something :
Authsession has a fonction : AuthSession.exchangeCodeAsync(config, discovery)

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...

这是唯一一个GitHub清楚地解释了如何在RN中使用KEYCLOAK设置身份验证...


I have a last error with android that I need to fix but it's ok !

我有最后一个错误的Android,我需要修复,但它是好的!


cheers

干杯


更多回答

Is this the answer?

这就是答案吗?

yes in typescript but you can use it with RN

可以在打印脚本中使用,但可以与RN一起使用

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