firebase - 未找到访问令牌受众中的 OAuth2 客户端 ID
问题描述
首先,我很抱歉我的英语不好。我使用 Expo 创建了一个 Android 应用程序。我通过 Firebase 实现了 Google 登录。它没有问题。为了将结果交付给客户,Firebase 项目的所有权移交给了客户。我将我的权限更改为编辑。
但有一个问题。
firebase.auth().signInWithCredential 函数导致错误。错误代码和信息如下。
auth/invalid-credential OAuth2 客户端 ID 在访问令牌 anaudience 中未找到。
我已经用谷歌搜索了错误消息来解决这个问题,但我无法找到它。这就是我在 Stack Overflow 上写作的原因。
登录屏幕.js
import React, { useEffect, useState } from 'react';
import { Button, Image, ImageBackground, Modal, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import * as Google from 'expo-google-app-auth';
import firebase from 'firebase'
import { fireAuth, fireStore } from '../Components/FireConfig';
import { screenHeight, screenWidth } from '../Components/Base';
import i18n from 'i18n-js';
import { ActivityIndicator } from 'react-native-paper';
export function LoginScreen({ navigation, route }) {
const [isLoading, setIsLoading] = useState(false)
// var provider = new firebase.auth.GoogleAuthProvider();
function isUserEqual(googleUser, firebaseUser) {
if (firebaseUser) {
var providerData = firebaseUser.providerData;
for (var i = 0; i < providerData.length; i++) {
console.log('providerData[i].providerId', providerData[i].providerId)
console.log('providerData[i].uid', providerData[i].uid)
if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
// providerData[i].uid === googleUser.getBasicProfile().getId()) {
providerData[i].uid === googleUser.user.id) {
// We don't need to reauth the Firebase connection.
return true;
}
}
}
return false;
}
function onSignIn(googleUser) {
// console.log('Google Auth Response', googleUser);
// We need to register an Observer on Firebase Auth to make sure auth is initialized.
var unsubscribe = fireAuth.onAuthStateChanged(function (firebaseUser) {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(googleUser, firebaseUser)) {
// Build Firebase credential with the Google ID token.
var credential = firebase.auth.GoogleAuthProvider.credential(
//googleUser.getAuthResponse().id_token
googleUser.idToken,
googleUser.accessToken,
)
console.log("credential", credential)
// Sign in with credential from the Google user.
fireAuth
.signInWithCredential(credential)
.then((result) => {
const uid = result.user.uid
if (result.additionalUserInfo.isNewUser) {
fireStore
.collection('users')
.doc(uid)
.set({
google_email: result.user.email,
google_profile_picture: result.additionalUserInfo.profile.picture,
google_locale: result.additionalUserInfo.profile.locale,
google_name: result.additionalUserInfo.profile.name,
created_at: Date.now(),
isPushInfo: false
})
} else {
fireStore
.collection('users')
.doc(uid)
.update({
last_logged_in: Date.now()
})
}
})
.catch((e) => {
console.log(e.code, e.message)
});
} else {
console.log('User already signed-in Firebase.', fireAuth.languageCode);
}
});
}
async function signInWithGoogleAsync() {
setTimeout(() => {
setIsLoading(true)
}, 500)
try {
const result = await Google.logInAsync({
androidClientId : "!!!.apps.googleusercontent.com",
androidStandaloneAppClientId : "@@@.apps.googleusercontent.com",
iosClientId : "###.apps.googleusercontent.com",
iosStandaloneAppClientId : "$$$.apps.googleusercontent.com",
scopes: ['profile', 'email']
});
if (result.type === 'success') {
console.log(result.type)
onSignIn(result)
return result.accessToken;
} else {
setIsLoading(false)
console.log(result)
alert(result)
return { cancelled: true };
}
} catch (e) {
setIsLoading(false)
console.log(e)
alert(e)
return { error: true };
}
}
return (
<View style={styles.container}>
<TouchableOpacity
style={{
alignItems: "center",
justifyContent: "center",
borderRadius: 10,
backgroundColor: "white",
width: screenWidth * 0.8,
height: 50
}}
title="Sign In With Google"
onPress={() => {
signInWithGoogleAsync();
}} >
<Text style={{ color: "#5887f9" }}>{i18n.t('SignInWithGoogle')}</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}
})
正如您从下面的链接中看到的那样
https://console.developers.google.com/apis/credentials
应用程序.json
{
"expo":
...
"android": {
"package": "com.mycompany.myapp",
"versionCode" : 2,
"permissions": [
"READ_EXTERNAL_STORAGE",
"READ_INTERNAL_STORAGE",
"WRITE_EXTERNAL_STORAGE",
"ACCESS_FINE_LOCATION",
"INTERNET",
"CAMERA_ROLL",
"CAMERA"
],
"googleServicesFile": "./google-services.json",
"useNextNotificationsApi": true,
"config": {
"googleMobileAdsAppId": "```",
"googleSignIn" :{
"apiKey" : "my apiKey",
"certificateHash" : "11:22:---:11:22"
}
}
}
...
}
}
API 密钥和 OAuth 2.0 客户端 ID 必须写入链接中的内容。
我期待着您的友好合作。
解决方案
关于 GitHub 上的这张票:https ://github.com/FirebaseExtended/flutterfire/issues/4053 Google 登录和 Firebase 身份验证存在一个核心问题。我联系了 GCP 支持,几个小时后他们修复了它❤️
推荐阅读
- svn - “签出修订版 0”是什么意思?
- sql - 如何在 BigQuery 中从 GeoJSON 中提取单独的值
- sql - SQL 数据透视表未将结果放在一行中
- android - 想写一个代码只在特定时间发送通知,但不知何故,alarmManager 不工作,它显示了一些错误
- excel - 根据具有不同数据源的不同工作表上的另一个数据透视表过滤器过滤数据透视表
- c++ - Qt printing on linux has wrong coordinates
- c# - registering clients and password
- android-contentprovider - 如何在我的应用程序图标(如whatsapp)的联系人详细信息中显示多次图标
- java - 使用 Ivy.xml 从 Github 解决依赖关系
- list - 询问用户在 Haskell 中的列表输入