首页 > 解决方案 > 无法在 Expo 上使用 React Native 获取刷新令牌,并使用带有 PKCE 的授权代码流向 Microsoft Azure 进行身份验证

问题描述

谁能得到这个工作?可以取回访问令牌,但不能取回刷新令牌。这是一个使用 React Native 和 Expo 构建的移动应用程序。它使用 Microsoft Azure 进行身份验证,并使用带有 PKCE 的授权代码流。

这是我尝试过的代码:-

// Endpoint
  const discovery = useAutoDiscovery('https://login.microsoftonline.com/TENNANT_ID/v2.0');
  // Request
  const [request, response, promptAsync] = useAuthRequest(
    {
      clientId: 'YOUR - CLIENT - ID',
      scopes: ['openid', 'profile', 'email', 'offline_access'],
      redirectUri: makeRedirectUri({
        scheme: 'exp://YOUR IP ADDRESS:19000'
        }),
        // codeChallenge: 'CODE_CHALLENGE',
        // codeChallengeMethod: 'S256',
        // usePKCE: true
      },
    discovery
  );

  React.useEffect(() => {
    if (response && 'params' in response) {
        if (response.params && 'code' in response.params) {

            (async function getToken() {
                console.log('-----------------------')
                console.log(response.params.code)
                console.log('-----------------------')

                try {
                    // @ts-ignore
                    const { accessToken } = await exchangeCodeAsync({
                        code: response.params.code,
                        clientId: 'YOUR - CLIENT - ID',
                        redirectUri: makeRedirectUri({
                          scheme: 'exp://YOUR IP ADDRESS:19000'
                          }),
                        scopes: ['openid', 'profile', 'email', 'offline_access'],
                        grant_type: "authorization_code",
                        extraParams: {
                          // code_verifier: "CODE_VERIFIER",
                          code_verifier: request?.codeVerifier || '' ,
                      },
                    }, {
                        tokenEndpoint: 'https://login.microsoftonline.com/TENNANT_ID/oauth2/v2.0/token' // 
                    })

                    console.log('------- Access ----------------')
                    console.log(accessToken)
                    console.log('-----------------------')

标签: azurereact-nativeauthenticationexpopkce

解决方案


推荐阅读