来自 Reactive Native 的 Google SignIn 给出 TypeError - Expo

Ska*_*osh 5 google-oauth reactjs react-native expo

我正在尝试使用 Google OAuth 登录创建一个简单的 React Native 应用程序。我已经在 google 中创建了凭据,并在 app.js 文件中输入了相同的凭据。

该应用程序在 android 模拟器中启动,当我单击使用 google 登录时,它在控制台中出现错误:

“错误 [TypeError: undefined is not an object (evaluating '_expo.default.Google')]”

我不知道如何解决这个问题。

这是我的 app.js

import React from "react"
import { StyleSheet, Text, View, Image, Button } from "react-native"
import Expo from "expo"

export default class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      signedIn: false,
      name: "",
      photoUrl: ""
    }
  }
  signIn = async () => {
    try {
      const result = await Expo.Google.logInAsync({
        androidClientId:
          "**********",
        //iosClientId: YOUR_CLIENT_ID_HERE,  <-- if you use iOS
        scopes: ["profile", "email"]
      })

      if (result.type === "success") {
        this.setState({
          signedIn: true,
          name: result.user.name,
          photoUrl: result.user.photoUrl
        })
      } else {
        console.log("cancelled")
      }
    } catch (e) {
      console.log("error", e)
    }
  }
  render() {
    return (
      <View style={styles.container}>
        {this.state.signedIn ? (
          <LoggedInPage name={this.state.name} photoUrl={this.state.photoUrl} />
        ) : (
          <LoginPage signIn={this.signIn} />
        )}
      </View>
    )
  }
}

const LoginPage = props => {
  return (
    <View>
      <Text style={styles.header}>Sign In With Google</Text>
      <Button title="Sign in with Google" onPress={() => props.signIn()} />
    </View>
  )
}

const LoggedInPage = props => {
  return (
    <View style={styles.container}>
      <Text style={styles.header}>Welcome:{props.name}</Text>
      <Image style={styles.image} source={{ uri: props.photoUrl }} />
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  },
  header: {
    fontSize: 25
  },
  image: {
    marginTop: 15,
    width: 150,
    height: 150,
    borderColor: "rgba(0,0,0,0.2)",
    borderWidth: 3,
    borderRadius: 150
  }
})
Run Code Online (Sandbox Code Playgroud)

任何帮助,建议?

非常感谢 !

mar*_*ttu 5

由于最新版本的 expo CLI 中所做的更改,文档中的此示例不再有效。

Google 应用程序身份验证包已移至独立包 -expo-google-app-auth因此,您需要从该包导入 Google 对象。

以下是确保您引用正确包的一些步骤

  • 确保已安装 expo-google-app-auth (您可以使用expo install expo-google-app-auth
  • 那么你应该使用以下命令导入 Google 对象:import * as Google from 'expo-google-app-auth'
  • 那么你可以像这样使用 logInAsync() 函数:
    ...
    const result = await Google.logInAsync({
    ...
    })
    
    Run Code Online (Sandbox Code Playgroud)


Ska*_*osh 0

我通过降级到 SDK expo 版本 31.0.0 解决了这个问题。