代码之家  ›  专栏  ›  技术社区  ›  Alex

谷歌登录在开发构建中工作,但不在预览构建中工作

  •  0
  • Alex  · 技术社区  · 3 年前

    我正在用expo和react native构建一个应用程序。我通过firebase使用谷歌登录。当我运行iOS开发构建时,登录功能可以完美工作。然而,当我运行iOS预览版本时,我收到以下错误:“访问被阻止:此应用程序的请求无效您无法登录,因为此应用程序发送了无效的请求。您可以稍后再试,或就此问题与开发人员联系。错误400:redirect_uri_mismatch”。

    当我点击“错误详细信息”时,它只显示“错误400:redirect_uri_mismatch”。

    在iOS版谷歌控制台客户端ID和firebase应用程序上,我的苹果标识符是com.username.appname,捆绑包ID是com.username.appname。

    当错误发生时,我试着插入设备并观看控制台,但我没有看到任何相关的内容。有人经历过这个错误吗?或者,当“developmentClient”为false时,是否有一些变量发生了变化,这可能会提供为什么会发生这种错误的线索?

    Screenshot of error message

    Screenshot of error details

    "dependencies": {
        "@react-native-async-storage/async-storage": "~1.17.11",
        "@react-navigation/native": "^6.1.2",
        "@react-navigation/native-stack": "^6.9.8",
        "expo": "^48.0.11",
        "expo-auth-session": "~4.0.3",
        "expo-dev-client": "~2.2.1",
        "expo-image-picker": "~14.1.1",
        "expo-linear-gradient": "~12.1.2",
        "expo-random": "~13.1.1",
        "expo-splash-screen": "~0.18.1",
        "expo-status-bar": "~1.4.4",
        "expo-web-browser": "^12.1.1",
        "firebase": "^9.16.0",
        "nativewind": "^2.0.11",
        "react": "18.2.0",
        "react-native": "0.71.6",
        "react-native-dotenv": "^3.4.7",
        "react-native-safe-area-context": "^4.5.1",
        "react-native-safearea-height": "^1.0.5",
        "react-native-screens": "^3.20.0"
      },
      "devDependencies": {
        "@babel/core": "^7.20.12",
        "tailwindcss": "^3.2.4"
      },
    
    //LoginScreen.js
    import React, { useEffect } from 'react';
    import * as Google from 'expo-auth-session/providers/google';
    import { IOS_CLIENT_ID, EXPO_CLIENT_ID, ANDROID_CLIENT_ID } from '@env';
    import { makeRedirectUri } from 'expo-auth-session';
    import {
      GoogleAuthProvider,
      signInWithCredential,
    } from '@firebase/auth';
    import { auth } from '../firebase';
    
    
    const LoginScreen = () => {
    
      const [request, response, promptAsync] = Google.useAuthRequest({
        iosClientId: IOS_CLIENT_ID,
        expoClientId: EXPO_CLIENT_ID,
        adroidClientID: ANDROID_CLIENT_ID,
        redirectUri: makeRedirectUri({ scheme: 'com.username.appname' }),
      });
    
      useEffect(() => {
        if (response?.type == 'success') {
          const idToken = response['authentication']['idToken'];
          const accessToken = response['authentication']['accessToken'];
          const credential = GoogleAuthProvider.credential(idToken, accessToken);
          signInWithCredential(auth, credential);
        }
      }, [response]);
    
      const signInWithGoogle = () => {
        promptAsync();
      };
    
    
    //eas.json
    {
      "cli": {
        "version": ">= 2.7.1"
      },
      "build": {
        "development": {
          "developmentClient": true,
          "distribution": "internal"
        },
        "preview": {
          "distribution": "internal"
        },
        "production": {}
      },
      "submit": {
        "production": {}
      }
    }
    
    //app.json
    {
      "expo": {
        "name": "appname",
        "slug": "appname",
        "scheme": "com.username.appname",
        "version": "1.0.0",
        "orientation": "portrait",
        "icon": "./assets/icon.png",
        "userInterfaceStyle": "light",
        "plugins": [
          [
            "expo-image-picker",
            {
              "photosPermission": "Allow access to photos to add photos.",
              "cameraPermission": "Allow access to camera to take photos."
            }
          ]
        ],
        "splash": {
          "image": "./assets/splashscreen.png",
          "resizeMode": "contain",
          "backgroundColor": "#000000"
        },
        "updates": {
          "fallbackToCacheTimeout": 0
        },
        "assetBundlePatterns": [
          "**/*"
        ],
        "ios": {
          "supportsTablet": true,
          "bundleIdentifier": "com.username.appname"
        },
        "android": {
          "adaptiveIcon": {
            "foregroundImage": "./assets/adaptive-icon.png",
            "backgroundColor": "#FFFFFF"
          },
          "package": "com.username.appname"
        },
        "web": {
          "favicon": "./assets/favicon.png"
        },
        "extra": {
          "eas": {
            "projectId": "1aaaa111-aa11-1aaa-aaaa-1a11aa1a11a1"
          }
        }
    
      }
    }
    
    
    //firebase.js
    import { initializeApp } from 'firebase/app';
    import { getAnalytics } from 'firebase/analytics';
    import { getFirestore } from 'firebase/firestore';
    import { getStorage } from 'firebase/storage';
    import {
      firebaseConfig_apiKey,
      firebaseConfig_authDomain,
      firebaseConfig_projectId,
      firebaseConfig_storageBucket,
      firebaseConfig_messagingSenderId,
      firebaseConfig_appId,
      firebaseConfig_measurementId,
    } from '@env';
    import {
      initializeAuth,
      getReactNativePersistence,
    } from 'firebase/auth/react-native';
    import AsyncStorage from '@react-native-async-storage/async-storage';
    
    const firebaseConfig = {
      apiKey: firebaseConfig_apiKey,
      authDomain: firebaseConfig_authDomain,
      projectId: firebaseConfig_projectId,
      storageBucket: firebaseConfig_storageBucket,
      messagingSenderId: firebaseConfig_messagingSenderId,
      appId: firebaseConfig_appId,
      measurementId: firebaseConfig_measurementId,
    };
    
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const auth = initializeAuth(app, {
      persistence: getReactNativePersistence(AsyncStorage),
    });
    const storage = getStorage(app);
    
    const db = getFirestore();
    
    export { auth, db, storage };
    

    enter image description here image showing ios client

    0 回复  |  直到 3 年前
        1
  •  2
  •   Alex    3 年前

    回答我自己的问题。。。经过几天的尝试,我解决了这个问题。我改变了两件事

    1. 在app.json中,我添加了“隐私”:“未列出”。我认为这可能不是问题的原因。
    2. 我在开发过程中的某个时候更改了我的项目名称,所以我的expo项目名称不再与我的捆绑包id对齐。我用新名称创建了一个新的expo工程,将我的秘密添加到expo工程中,重新配置了构建,创建了新的“预览”构建,瞧!它在开发构建和预览构建中都能顺利工作。

    我希望有一天这能帮助其他人。

        2
  •  0
  •   Moti Charlap    3 年前

    当使用开发构建时,您使用expo-go连接到您的google帐户,即Web客户端(expoClientId),但在预览中,您使用的是iosClientId,我猜问题就在那里,因为您显示的是Web客户端配置而非Ios客户端配置的图片,也许您仍然需要在那里添加重定向链接

    推荐文章