Google People API:为什么以数组格式返回生日

问题描述

任何给定的 Google 帐户都应该有 1 个生日。
在使用people API检索用户生日时,意外发现检索到的生日响应不一致。
即:为两个不同帐户检索的响应在结构上不相同。

我注意到这一点:

生日可以是 2 或 1 个生日的数组。
我注意到如果是 2 个生日数组中的第二个值包含年份。
而第一个由于某种原因只包含月和日。

enter image description here


如果是 1 个生日,那么它包含年、月、日。

enter image description here

const validateGoogleAccesstokenAndReturnPersonalInfo = async (
  tokenId,accesstoken
) => {
  try {
    const CLIENT_ID =
      "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx.apps.googleusercontent.com";
    const client = new oauth2client(CLIENT_ID);

    const ticket = await client.verifyIdToken({
      idToken: tokenId,audience: CLIENT_ID,});

    const payload = ticket.getPayload();

    const { OAuth2 } = google.auth;
    const oauth2client = new OAuth2();
    oauth2client.setCredentials({ access_token: accesstoken });

    const peopleAPI = google.people({
      version: "v1",auth: oauth2client,});

    const { data } = await peopleAPI.people.get({
      resourceName: "people/me",personFields: "birthdays",});

    const {birthdays} = data;
    let  birthday

   

    if (birthdays) {
      /**Birthday
      A person's birthday. At least one of the date and text fields are specified. The date and text fields typically represent the same date,but are not guaranteed to.
      #Todo: What if text is there.
      */

      // #NOTE:
      // biryhdays can be an array of 2 or 1 birthdays
      // I noticed if it's 2 birthdays
      // The second value in the array contains the year
      // While the first one contains only month and day for some reason
      // If it's 1 birthday
      // Then it contains year,month,day
      // For this reason I'll be using birthdays.length - 1
      // I am not sure this is applicable to all accounts though
      // So I need to verify it

      const { year,day } = birthdays[birthdays.length - 1].date;
      birthday = year + "-" + month + "-" + day;
    }

   

    const response = {
      birthday: birthday,};
    return response;
  } catch (error) {
    console.log("error: ",error);
    throw new Error("Google token validation Failed");
  }
};

所以我不得不做个小把戏来验证我是否成功获得了用户的生日:

出于这个原因,我将使用birthdays.length - 1 我不确定这个 虽然适用于所有帐户所以我需要验证它

这里:

const { year,day } = birthdays[birthdays.length - 1].date;

这种方法的问题在于我不确定它是否适用于所有帐户。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)