reactjs - 从 React Native 登录 Google 时出现 TypeError - Expo

标签 reactjs react-native google-oauth expo

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

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

"error [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
  }
})

有什么帮助、建议吗?

非常感谢!

最佳答案

由于最新版本的 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({
    ...
    })
    

关于reactjs - 从 React Native 登录 Google 时出现 TypeError - Expo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56691959/

相关文章:

javascript - 如何在 React 中打印 JavaScript new Date()?

reactjs - 跨域读取阻塞 (CORB) 阻止了 React 应用程序上的跨域响应

android - 无法获取博览会推送 token

android - 有什么方法可以检测到 react native(expo) 中的 android 软导航栏?

java - 错误 403 禁止访问(权限不足)与 Google Cloud Storage JSON API Java 示例

javascript - 在 AJAX 请求后,如何使用 react 上下文 api 值更新 Formik 初始值?

ios - react native : SyntaxError: Strict mode does not allow function declarations in a lexically nested statement

C# 谷歌驱动器 : Allow users read access to my drive

php - Google OAuth JWT 签名验证

javascript - React Native 组件未在 map() 迭代器内渲染