如何修复 Google expo auth 会话登录错误 "Something went wrong when trying to finish signing in"

How to fix Google expo auth session sign in Error "Something went wrong when trying to finish signing in"

我正在尝试使用 expo-auth-session 在我的 expo 中实现 google 登录, 当我点击我的 gmail 登录时,我被重定向到这个 screen 说“尝试完成登录时出了点问题。请关闭此屏幕以返回应用程序”。

//Google auth code:
    import * as Google from 'expo-auth-session/providers/google';
    const [request, response, promptAsync] = Google.useAuthRequest({
        expoClientId: config.google.expoClientId,
        redirectUri: config.google.redirectUri,
      });

      React.useEffect(() => {
      //Handle google login
      console.log(response)
      if (response?.type === 'success') {
       const { authentication } = response;
      } 
      }, [response]);

    //Button that calls the google sign in
    <Button iconName={'google'} iconPressed={() => promptAsync({useProxy: true})} />

我最终使用了 expo-google-app-auth,出于某些我尚未弄清楚的原因,您必须使用 host.expo.exponent 作为 google 中的包名称和包标识符这个库的开发者控制台可以工作。 代码:

import { Alert } from 'react-native';
import * as Google from 'expo-google-app-auth'

const GoogleLogin = async () => {
    //Get those keys from the google developer console
    const { iosClientId, androidClientId } = config.google
    const { type, user } = await Google.logInAsync({
        iosClientId,
        androidClientId,
    });

    if (type === 'success') {
        /* `accessToken` is now valid and can be used to get data from the Google API with HTTP requests */
        return { email: user.email, id: user.id }
    } else {
        Alert.alert("Google login error.")
    }
}

export default GoogleLogin;

我想你可以这样试试

import * as Google from 'expo-auth-session/providers/google';
import * as WebBrowser from 'expo-web-browser';

WebBrowser.maybeCompleteAuthSession();
....
const [request, response, promptAsync] = Google.useAuthRequest({
        androidClientId: config.androidClientId,
        iosClientId: config.iosClientId,
        expoClientId: config.expoClientId,
        scopes: config.scopes,
    });

useEffect(() => {
   if (response?.type === 'success') {
       const { authentication } = response;
       getGoogleUser((authentication as any).accessToken)
   }
}, [response]);

const getGoogleUser = async (accessToken: string) => {

  try{
        const response = await fetch('https://www.googleapis.com/userinfo/v2/me', {
            headers: { Authorization: `Bearer ${accessToken}`}
        });

        const user = response.json()
        if (user?.email) {
            const { email, name } = user; // you will get more data in the user object
            .......
        }
    }
    catch(error){
        console.log('GoogleUserReq error: ', error);
    }
}

 return (
    <View>
      <Button 
        onPress={() => promptAsync() }
      />
    </View>
  );