【问题标题】:Trying to get birthday and gender with Google People API in Angular尝试在 Angular 中使用 Google People API 获取生日和性别
【发布时间】:2020-06-05 11:56:23
【问题描述】:

我已经尝试了几个小时,但我永远无法获得我想要获得的数据。 我需要以其他方式提出请求吗?这是我的代码:

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请求中得到的数据只有:

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

我应该再得到 2 个包含生日和性别的对象,但它永远不会发生。

【问题讨论】:

  • 您需要将从 firebase auth 结果收到的访问令牌传递给 people api
  • 我该怎么做???

标签: angular typescript google-people-api


【解决方案1】:

使用 Sebas 他的回答,我设法让 People API 与我的 Firebase 代码一起工作,我会在这里发布给其他人:

第一个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))
        })

【讨论】:

    【解决方案2】:

    我已经解决了这个问题,50% 感谢 Vilsad P P 在帖子下方的评论中。

    我搜索了如何将访问令牌放在url中,过了一段时间我找到了参数“access_token”,就像这样把它放在url中:

    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)
    

    【讨论】:

    • 什么是 API 密钥?如果不需要 oAuth,我相信
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    相关资源
    最近更新 更多