如何从 Expo GoogleSignIn API (expo v32) 获取 idToken?

2024-01-29

我正在尝试使用 Expo 在 React-Native 上设置本机 Google 身份验证谷歌登录 https://docs.expo.io/versions/v32.0.0/sdk/google-sign-in/API,但无法获取授权 Google 用户的 idToken。

它有 accessToken 作为响应,但没有 idToken。

所以我使用一些简单的代码,例如

const result = await GoogleSignIn.initAsync({
                isOfflineEnabled: true,
                isPromptEnabled: true,
                //webClientId: 'webClientId',
                clientId // now testing only on Android, so this is irrelevant
                });
console.log(result);

响应示例:

 Object {
    "auth": Object {
      "accessToken": "accessToken",
      "accessTokenExpirationDate": null,
      "idToken": null,  // here is the problem!
      "refreshToken": null,
    },
    "displayName": "Danila Tsvetkov",
    "email": "email@email",
    "firstName": "Danila",
    "hostedDomain": undefined,
    "lastName": "Tsvetkov",
    "serverAuthCode": null,
    "uid": "uid",
  }

同时Google https://docs.expo.io/versions/v32.0.0/sdk/google/OAuth API不仅返回accessToken,还返回idToken和refreshToken。

其他一切都工作正常,例如授权和登录流程。

也许问题出在 serverAuthCode 上?

尝试放置 webClientId,api 停止正常工作。将 SHA1 添加到 google-services (更多信息 https://github.com/react-native-community/react-native-google-signin/issues/224),没有帮助。更改其他参数(例如“isOfflineEnabled”)也没有多大作用。


你能用这个吗

const result = await Google.logInAsync({
          androidClientId: "android id"
          iosClientId: 'Ios Id',
          scopes: ['profile', 'email'],
        });

then

 if (result.type === 'success') {
          console.log(result.idToken);
//or
          Alert.alert(result.idToken);
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Expo GoogleSignIn API (expo v32) 获取 idToken? 的相关文章

随机推荐