首页 > 解决方案 > react native expo:谷歌身份验证不返回用户数据

问题描述

我在这里按照官方文档中的流程进行操作

https://docs.expo.io/versions/latest/sdk/google

我的源代码看起来像这样

export async function signInWithGoogleAsync() {
  try {
    const result = await Expo.Google.logInAsync({
      androidClientId: '272284064749-p61ji1pgisvk1d5s2k7kc56kch0vssi9.apps.googleusercontent.com',
     // iosClientId: '272284064749-p61ji1pgisvk1d5s2k7kc56kch0vssi9.apps.googleusercontent.com',
      scopes: ['profile', 'email'],
    });

    if (result.type === 'success') {
      return getUserInfo(result.accessToken);
    } else {
      return {cancelled: true};
    }
  } catch(e) {
    return {error: true};
  }
}

获取用户信息:

async function getUserInfo(accessToken) {
  let Info = await fetch('https://www.googleapis.com/userinfo/v2/me', {
    headers: { Authorization: `Bearer ${accessToken}`},
  });

  return console.log(JSON.stringify(Info));
}

令人惊讶的是,它提供用户数据而不是将其记录到控制台

{"type":"default","status":200,"ok":true,"headers":{"map":{"alt-svc":"quic=\":443\"; ma=2592000; v=\"44,43,39,35\"","x-content-type-options":"nosniff","cache-control":"public, max-age=0","x-frame-options":"SAMEORIGIN","server":"ESF","vary":"Referer","date":"Thu, 25 Oct 2018 05:14:19 GMT","x-xss-protection":"1; mode=block","content-type":"application/json; charset=UTF-8","expires":"Mon, 01 Jan 1990 00:00:00 GMT"}},"url":"https://www.googleapis.com/userinfo/v2/me","_bodyInit":{"_data":{"size":370,"offset":0,"blobId":"bfbedf5e-e2d7-45f0-b97c-483f814307dc"}},"_bodyBlob":{"_data":{"size":370,"offset":0,"blobId":"bfbedf5e-e2d7-45f0-b97c-483f814307dc"}}}

标签: react-nativeexpo

解决方案


Expo 26.0.0 以后,google 用户信息作为result对象的一部分出现。正如您可以访问一样result.accessToken,您可以使用 访问用户信息result.user

希望它可以帮助您和其他面临此问题的人。


推荐阅读