尝试使用 Angular 中的 Google People API 获取生日和性别 [英] Trying to get birthday and gender with Google People API in Angular
本文介绍了尝试使用 Angular 中的 Google People API 获取生日和性别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经花了几个小时尝试这样做,但我永远无法获得我想要获得的数据.我需要以其他方式或其他方式提出请求吗?这是我的代码:
I've been some hours trying to do this, but i can't never get the data i'm trying to get. Do i need to do the request other way or something? there's my code:
doGoogleLogin(){
return new Promise<any>((resolve, reject) => {
let provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
provider.addScope('https://www.googleapis.com/auth/user.birthday.read');
provider.addScope('https://www.googleapis.com/auth/user.gender.read');
this.afAuth.auth
.signInWithPopup(provider)
.then(res => {
resolve(res);
console.log(res.additionalUserInfo.profile['id']);
this.http
.get("https://people.googleapis.com/v1/people/"+res.additionalUserInfo.profile['id']+"?key=(a valid api key)&personFields=birthdays,genders")
.subscribe(data => (console.log(data)));;
}, err => reject(err))
})
}
我通常从http请求中得到的数据只有:
What i usually get in data, from the http request is only:
{
"resourceName": "people/101296298961277932659",
"etag": "%EgQBBzcuGgQBAgUH"
}
而且我应该再得到 2 个包含生日和性别的对象,但它从未发生过.
And i should be getting 2 more objects containing the birthdate and gender, but it never happen.
推荐答案
使用 Sebas 他的回答,我设法让 People API 与我的 Firebase 代码一起工作,我将在此处发布给其他人:
Using Sebas his answer I managed to get the People API working with my Firebase code, I'll post it here for others:
import firebase from 'firebase/app'
let provider = new firebase.auth.GoogleAuthProvider()
provider.addScope('profile')
provider.addScope('email')
provider.addScope('https://www.googleapis.com/auth/user.birthday.read')
provider.addScope('https://www.googleapis.com/auth/user.gender.read')
firebase
.auth()
.signInWithPopup(provider)
.then(res => {
fetch(
`https://people.googleapis.com/v1/people/${res.additionalUserInfo.profile.id}?personFields=birthdays,genders&access_token=${res.credential.accessToken}`
).then(response => console.log(response))
})
这篇关于尝试使用 Angular 中的 Google People API 获取生日和性别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文