react-native - 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"}}}
解决方案
Expo 26.0.0 以后,google 用户信息作为result
对象的一部分出现。正如您可以访问一样result.accessToken
,您可以使用 访问用户信息result.user
。
希望它可以帮助您和其他面临此问题的人。
推荐阅读
- javascript - 从 GUI 重写 js 代码以在 webapp 中使用
- python - 如何让我的不和谐机器人响应正确答案?
- scala - 数据集上的 avg() 是否产生最有效的 RDD?
- ios - 在 SwiftUI 中调出 Google 登录屏幕
- php - 在 Laravel 5.7 中,如何处理作为集合的子集合的集合?
- angular - ionic 4 - 离子滑动组件不工作
- css - SCSS - 作为父母的第 n 个孩子?
- python - 让 MATLAB R2019b 使用 Python3.6
- excel - If (Months.Columns(1).Value = "Oct" or "Jan" or "Apr" or "Jul") Then ---- 类型不匹配我该如何解决?
- python - Django 值错误。模型字段必须是一个实例