如何从谷歌登录获取性别和国家详细信息?

问题描述

在我的 React Native 项目中,我使用 firebase 实现了 Google 登录功能。当我使用 Google 登录时,我得到了成功的响应。

GoogleSignInMethods

export const googleConfigure = () =>
  GoogleSignin.configure({
    webClientId: FIREBASE_WEB_CLIENT_ID,offlineAccess: true,forceCodeForRefreshToken: true,accountName: '',});

export const signIn = async () => {
  try {
    await GoogleSignin.hasPlayServices();
    const info = await GoogleSignin.signIn();
    console.warn({userInfo: info});
    // set user into state
  } catch (error) {
    if (error.code === statusCodes.SIGN_IN_CANCELLED) {
      console.log('Sign in cancelled');
    } else if (error.code === statusCodes.IN_PROGRESS) {
      console.log('Sign in inprogress');
    } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
      console.log('Play service not available in this device');
    } else {
      console.log('Something error happened',error);
    }
  }
};

用户信息

user {
  email: "data comes here"
  familyName: "data comes here"
  givenname: "data comes here"
  id: "data comes here"
  name: "data comes here"
  photo: "data comes here"
}

对于我的项目,我需要用户的性别和国家/地区详细信息。我怎样才能找回它?

解决方法

您无法使用 Google 登录来检索用户的性别以获取 react-native,因为该软件包没有实现此类功能。您可以做的是使用您从调用登录函数中收到的令牌调用另一个 google api

await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
if (userInfo) {
  const { accessToken } = await GoogleSignin.getTokens();
  const response = await axios({
    method: 'GET',headers: {
      Authorization: `Bearer ${accessToken}`,},url: `https://people.googleapis.com/v1/people/${userInfo.user.id}?personFields=genders`,});
  console.log(response); // You should have the info here
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...