反应本地博览会:Google auth不返回用户数据

时间:2018-10-25 05:20:52

标签: 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"}}}

1 个答案:

答案 0 :(得分:2)

Expo 26.0.0及以后,google用户信息是result对象的一部分。 就像您可以访问result.accessToken一样,您也可以使用result.user访问用户信息。

希望它对您和其他遇到此问题的人都有帮助。