我一直在try 将社交登录整合到我的react native项目中,在该项目中,我成功登录facebook,但未能登录到谷歌.react-native-google-signin库用于谷歌.

我用过的代码.

componentDidMount() {
GoogleSignin.hasPlayServices({ autoResolve: true }).then(() => {
// play services are available. can now configure library
}).catch((err) => {

console.log("Play services error", err.code, err.message);
})
GoogleSignin.configure({
scopes: ["https://www.googleapis.com/auth/drive.readonly"], // what API you want to access on behalf of the user, default is email and profile
// iosClientId: <FROM DEVELOPER CONSOLE>, // only for iOS
webClientId: "xxx", // client ID of type WEB for your server (needed to verify user ID and offline access)
// offlineAccess: true // if you want to access Google API on behalf of the user FROM YOUR SERVER
//hostedDomain: '' // specifies a hosted domain restriction
//forceConsentPrompt: true // [Android] if you want to show the authorization prompt at each login
//accountName: '' // [Android] specifies an account name on the device that should be used
})
.then(() => {
// you can now call currentUserAsync()
});
    _signIn = async () => {

        try {

            await GoogleSignin.hasPlayServices(

              )

          const userInfo = await GoogleSignin.signIn();
          console.log('User Info --> ', userInfo);
          this.setState({ userInfo });
        } catch (error) {

          console.log('Message', error.message);
          if (error.code ===  statusCodes.SIGN_IN_CANCELLED) {
            console.log('User Cancelled the Login Flow');
          } else if (error.code === statusCodes.IN_PROGRESS) {
            console.log('Signing In');
          } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
            console.log('Play Services Not Available or Outdated');
          } else {
            console.log('Some Other Error Happened');
          }
        }
      };

错误响应:

Message: A non-recoverable sign in failure occurred -catch error

推荐答案

那是因为客户身份.

首先创建一个新项目,如下图所示

然后从列表中 Select 该项目以创建凭据enter image description here

这对我很管用.

来登录配置

GoogleSignin.hasPlayServices({ autoResolve: true }).then(() => {

})
.catch((err) => {
      console.log("Play services error", err.code, err.message);
})

GoogleSignin.configure({
     scopes: ["https://www.googleapis.com/auth/userinfo.profile"],//scopes as you need 
     webClientId: "***(Your clientId)",
     //iosClientId: <FROM DEVELOPER CONSOLE>, // only for iOS 
     //offlineAccess: true, //(give it true if you need serverAuthCode i.e cross client authorisation)
     //hostedDomain: '' 
     //forceConsentPrompt: true // [Android] if you want to show the authorization prompt at each login
     //accountName: ''
})

React-native相关问答推荐

如何在底部标签导航中添加顶部标签

使用 React Native 下载数据文件以供离线使用

react native :navigationOptions 中的标题样式不起作用

reactnavigation后退按钮的自定义后退导航

React Native 响应式图像宽度,而图像的宽度/高度比保持不变

redux-observable 您在预期流的地方提供了 undefined

ReactJS toLowerCase 不是一个函数

如何使用 AsyncStorage React Native 缓存 API 数据

create-react-native-app myproject和react-native init myproject之间的真正区别是什么

如何以编程方式在 react-native 中截屏

如何在 React Native 中本地存储不是字符串的数据

@react-navigation/stack 与 @react-navigation/native-stack 有什么区别?

SyntaxError: const 声明中缺少初始化程序

如何使用 Stripe (stripe.js) 和 react-native

react-native图标

如何将 Tensorflow 与 react-native 一起使用?

更改按钮 colored颜色 onPress(切换功能)

我可以从react-native元素中删除 tvOS 吗?

如何将样式传递给 React-Native 中的容器组件

无法读取未定义的属性 string| React.PropTypes | LayoutPropTypes.js