firebase Google Login在开发版本中工作,但不是预览版本

jjhzyzn0  于 2023-04-22  发布在  Go
关注(0)|答案(2)|浏览(104)

我正在使用expo和react native构建一个应用程序。我正在通过firebase使用Google登录。当我运行iOS开发版本时,登录功能工作正常。然而,当我运行iOS预览版本时,我得到以下错误:“访问被阻止:此应用的请求无效您无法登录,因为此应用发送了无效的请求。您可以稍后重试,或联系开发人员解决此问题。错误400:redirect_uri_mismatch”。
当我点击“错误详细信息”时,它只显示“错误400:redirect_uri_mismatch”。
我的苹果标识符是com.usernames.appname,我的捆绑包ID是com.usernames.appname,在iOS和firebase应用程序的Google控制台客户端ID上都是如此。
我已经尝试插入设备并在发生错误时查看控制台,但我没有看到任何相关内容。有人遇到过此错误吗?或者当“developmentClient”为false时,是否有某个变量会发生变化,这可能会提供有关此错误发生原因的线索?

"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 };

de90aj5v

de90aj5v1#

回答我自己的问题......经过几天的尝试不同的事情我解决了这个问题。我改变的两件事是
1.在app.json中添加了“privacy”:“未上市”。我想这可能不是问题的原因。
1.我在开发过程中更改了我的项目名称,所以我的expo项目名称不再与我的bundle id一致。我用新名称创建了一个新的expo项目,将我的秘密添加到expo项目中,重新配置构建,创建了一个新的“预览”构建,瞧!它在开发构建和预览构建中都可以顺利运行。
我希望这能帮助别人有一天。

wmvff8tz

wmvff8tz2#

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

相关问题