如何从EXPO GoogleSignIn API(EXPO V32)获取idToken?

人气:811 发布:2022-10-16 标签: react-native google-signin expo

问题描述

我正在尝试使用EXPOGoogleSignInAPI在REACT-Native上设置本地Google身份验证,但无法获取授权Google用户的idToken。

它的响应中有accesToken,但没有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",
  }

同时GoogleOAuth接口不仅返回accesToken,还返回idToken和renhToken。

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

可能问题出在serverAuthCode?

尝试放置webClientID,API停止正常工作。将SHA1添加到Google-Services(more info),但没有帮助。更改其他参数(如"isOfflineEnabled")也起不了多大作用。

推荐答案

if (result.type === 'success') {
     console.log(result.user.auth);
}

这是因为我们使用的是google-sign-in,而不是expo-google-app-auth

所以总而言之,使用Result.user.auth,但在此之前,您的代码应该如下所示

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
signInAsync = async () => {
    try {
      await GoogleSignIn.askForPlayServicesAsync();
      const result = await GoogleSignIn.signInAsync();
      if (result.type === 'success') {
        
        this.onSignIn(result.user.auth);
        return result.user.auth;
      }
    } catch ({ message }) {
      alert('login: Error:' + message);
    }
  };

963