Trying to get birthday and gender with Google People API in Angular
Asked Answered
H

2

2

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))
    })
  }

What i usually get in data, from the http request is only:

{
  "resourceName": "people/101296298961277932659",
  "etag": "%EgQBBzcuGgQBAgUH"
}

And i should be getting 2 more objects containing the birthdate and gender, but it never happen.

Hoashis answered 5/6, 2020 at 11:56 Comment(2)
you need to pass the access token received from the firebase auth result to the people apiCyanine
how can i do that???Hoashis
B
3

Using Sebas his answer I managed to get the People API working with my Firebase code, I'll post it here for others:

First Enable the API in the Developer Console.

    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))
        })
Belgian answered 31/12, 2020 at 0:18 Comment(0)
H
1

I've resolved this, 50% thanks to Vilsad P P in the comment below the post.

I searched how to put the access token in the url, and after some time i found the parameter "access_token", just put it in the url like this:

https://people.googleapis.com/v1/people/(user_id)?personFields=(fields you want)&key=(valid api key)&access_token=(accessToken in credetials in the result of the authentification)
Hoashis answered 5/6, 2020 at 13:3 Comment(2)
what's API key? In case of oAuth that isn't required I believeDuhamel
@ShivamSahil Fetching data from people API requires a valid api keyScurrilous

© 2022 - 2024 — McMap. All rights reserved.