expo-auth-session/providers/google Google.useAuthRequest

Had*_*ert 9 google-oauth react-native expo

我在使用 Expo 管理的 React Native 应用程序中实施 Google Auth 时遇到问题。当我尝试登录时,响应不包含 IdToken 或用户信息,我不明白为什么......

这是我的代码:

 import * as Google from 'expo-auth-session/providers/google';
 
 const [request1, response1, promptAsync1] = Google.useAuthRequest({
    expoClientId: 'my-expo-id',
    iosClientId: 'my-ios-id',
  });

  React.useEffect(() => {
    if (response1?.type === 'success') {
      const { authentication } = response1;
    }
  }, [response]);

  console.log('reponse', response1)
  
  return (
  <View>
          <TouchableOpacity onPress={() => promptAsync1()}>
            <Text style={styles.connexionText}>Connect with Google</Text>
          </TouchableOpacity>
  </View>
  
  )
Run Code Online (Sandbox Code Playgroud)

这是回应:

reponse Object {
  "authentication": TokenResponse {
    "accessToken": "ya29.a0AfH6SMAEdPx5RcQP57rtdr4gV8GxFD1VSLAjovOce5X1yP-a2S6inLcSHF3KlxqmbKt0Cl6Catuyuua9Jz0rtV5psUUqWWX_QT32rXJwt9LTQywQaOzyy4cwspbOLm6W063w27f7NRiizC9RBg69-Yh09OhN",
    "expiresIn": "3599",
   ** "idToken": undefined, **
    "issuedAt": 1617701320,
    ** "refreshToken": undefined, **
    "scope": "email profile https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email",
    "state": "RwgjNwizdQ",
    "tokenType": "Bearer",
  },
  "error": null,
  "errorCode": null,
  "params": Object {
    "access_token": "ya29.a0AfH6SMAEdPx5RcQP57rtdr4gV8GxFD1VSLAjovOce5X1yP-a2S6inLcSHF3KlxqmbKt0Cl6Catuyuua9Jz0rtV5psUUqWWX_QT32rXJwt9LTQywQaOzyy4cwspbOLm6W063w27f7NRiizC9RBg69-Yh09OhN",
    "authuser": "0",
    "exp://172.20.10.3:19000/--/expo-auth-session": "",
    "expires_in": "3599",
    "prompt": "none",
    "scope": "email profile https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email",
    "state": "RwgjNwizdQ",
    "token_type": "Bearer",
  },
  "type": "success",
  "url": "exp://172.20.10.3:19000/--/expo-auth-session#state=RwgjNwizdQ&access_token=ya29.a0AfH6SMAEdPx5RcQP57rtdr4gV8GxFD1VSLAjovOce5X1yP-a2S6inLcSHF3KlxqmbKt0Cl6Catuyuua9Jz0rtV5psUUqWWX
_QT32rXJwt9LTQywQaOzyy4cwspbOLm6W063w27f7NRiizC9RBg69-Yh09OhN&token_type=Bearer&expires_in=3599&scope=email%20profile%20https://www.googleapis.com/auth/userinfo.profile%20openid%20https:/
/www.googleapis.com/auth/userinfo.email&authuser=0&prompt=none",
}
Run Code Online (Sandbox Code Playgroud)

预先非常感谢您的帮助!

how*_*ard 8

因此,如果您正在寻找 idToken,那么就有可能获得 idToken。您需要像这样修改您的代码:

     const [request, response, promptAsync] = Google.useAuthRequest({
        *responseType: "id_token",*
        expoClientId: 'my-expo-id',
        iosClientId: 'my-ios-id',
      });
Run Code Online (Sandbox Code Playgroud)

您还必须访问“params”键而不是“authentication”,这将显示大部分为空:)。对我来说它至少有效,因为其余信息毫无用处。哈!

编辑:我意识到我需要获得访问令牌才能在我的应用程序中使用谷歌驱动器,因此现在我需要这两个令牌并在此处提交了错误报告https://github.com/expo/expo/issues/12808来尝试解决这个问题。

  • 您是如何获得这些信息的?我遇到了同样的问题,花了一整天的时间寻找这个,我只在这里找到了它。顺便说一句,谢谢你救了我! (3认同)