expo-auth-session/providers/google 无法在独立应用程序上运行:invalid_scheme

expo-auth-session/providers/google not working on standalone app: invalid_scheme

我希望有人能帮助我解决我一直在努力解决的以下问题。

我正在尝试在 Expo SDK 39 中对 Android 的 Google 身份验证使用 expo-auth-session/providers/google。

我已遵循指南 here 但我所做的一切都以 google 消息结束:

Error 400: invalid_request Invalid parameter value for redirect_uri: Invalid scheme: com.my_domain.myapp:/oauthredirect

我只是不明白我做错了什么。

我需要指出的是,在 Expo Client 中,login/signup 使用完全相同的代码可以完美运行。

我无数次检查了凭据,令我困扰的是它在 expo 客户端中有效,但在独立应用程序中无效,所以我确定它与 google 配置有关,但我不知道不知道是什么。

这是我的 app.json:

{
  "expo": {
    ...
    "scheme": "myapp",
    "platforms": [
      "ios",
      "android"
    ],
    ...
    "android": {
      "package": "com.my_domain.myapp",
      ...
    }
  }
}

还有我的整合:

import React, { useState } from 'react';
import * as WebBrowser from "expo-web-browser";
import * as Google from 'expo-auth-session/providers/google';
import PillButton from '../UI/PillButton';

const SignupCmp = props => {
    const [browserOpen, setBrowserOpen] = useState(null);

    const [request, response, promptAsync] = Google.useAuthRequest({
        expoClientId: '123-myuniqueid.apps.googleusercontent.com',
        androidClientId: '123-myuniqueid.apps.googleusercontent.com'
    });

    useEffect(() => {
        WebBrowser.warmUpAsync().then(r => console.log('warmed'));

        return () => {
            WebBrowser.coolDownAsync().then(r => console.log('cooled'));
        };
    }, []);

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


    const handleBrowserOpen = async (url) => {
        let result = await WebBrowser.openBrowserAsync(url );
        setBrowserOpen(result);
    }

    return (
        <View>
            <PillButton
                disabled={!request}
                title={I18n.t('authScreens.signupWithGoogle')}
                type="solid"
                onPress={() => { promptAsync().then(async val => {
                  
                  props.googleAuth(val.authentication.accessToken);
                }) }}
            />
        </View>
    );
};

export default SignupCmp;

因为我没有想法,所以我真的需要帮助。

更新

我更新了 useAuthRequest 中的 redirectUri 属性 如下:

const [request, response, promptAsync] = Google.useAuthRequest({
    expoClientId: '123-myuniqueid.apps.googleusercontent.com',
    androidClientId: '1234-myuniqueid.apps.googleusercontent.com',
    redirectUri: AuthSession.makeRedirectUri({
      native: 'myapp:/oauthredirect',
      useProxy: true
    }),
    scopes: [
      'profile',
      'email'
    ]
  }, {});

现在消息已更改为

Invalid parameter value for redirect_uri: Missing authority: myapp:/oauthredirect

expo 客户端中的登录仍然正常。

我尝试在 Google 控制台中创建一个新的 API 密钥,并在那里尝试了不同的设置,但似乎没有任何效果。

我真的需要一些帮助。 谢谢!

我最终使用 import * as GoogleSignIn from 'expo-google-sign-in'; 而不是 import * as Google from 'expo-auth-session/providers/google';

看来这是我让它正常工作的唯一方法。

确保您没有运行您的展会带有离线标签 “expo start --offline” ---- 这是错误的 而是以“expo start”开始......这就是我在 android

上解决问题的方式

// 代码如下

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

    const [request, response, promptAsync] = Google.useAuthRequest({
             expoClientId: 'xxxxxxxx', //get this from google console
      
    });

     useEffect(() => {
     if (response?.type === 'success') {
       const { authentication } = response;
       // an access token will be returned in the authentication object
           getGoogleUser(authentication.accessToken)
      }
     }, [response]);

     const getGoogleUser = async (accessToken) => {
       try{
         let gUserReq = await 
           axios.get('https://www.googleapis.com/oauth2/v2/userinfo',
           {
            headers: {
                Authorization: `Bearer ${accessToken}`
            }
           }
         );
    
    console.log(gUserReq.data)
}
catch(error){
    console.log('GoogleUserReq error: ', error);
}

}