首页 > 解决方案 > Google People Api:地址字段返回“未定义”

问题描述

我正在尝试通过在此处指定的 People Api 请求中指定“地址”字段来获取经过身份验证的用户的国家/地区。这是代码:

router.post("/test_scope", (req, res) => {
  const { idToken, accessToken } = req.body;
  authenticationServices
    .validateGoogleAccessToken(idToken, accessToken)
    .then((response) => {
      res.json(response);
    });
});


const validateGoogleAccessToken = async (idToken, accessToken) => {
  try {
    const CLIENT_ID =
      "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com";
    const client = new OAuth2Client(CLIENT_ID);

    const ticket = await client.verifyIdToken({
      idToken,
      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,genders,ageRanges,addresses",
    });

    const { birthdays, genders, addresses, ageRanges } = data;

    console.group("AuthenticationServices.validateGoogleAccessToken");
    console.log("birthdays: ", birthdays); // returned correctly
    console.log("genders: ", genders); // returned correctly
    console.log("ageRanges: ", ageRanges); // returned correctly
    console.log("addresses: ", addresses); // undefined
    console.groupEnd();
    return data;
  } catch (error) {
    console.log("error: ", error);
    throw new Error("Google token validation failed");
  }
};

所有字段都在这里请求:

personFields: "birthdays,genders,ageRanges,addresses",

正确返回,但地址字段记录为未定义。
这是我发送请求后得到的全部响应:

{
  "resourceName": "people/XXXXXXXXXXXXXXXXx",
  "etag": "%XXXXXXXXXXXXXXXXXXXXXXXXXX",
  "genders": [
      {
          "metadata": {
              "primary": true,
              "source": {
                  "type": "PROFILE",
                  "id": "XXXXXXXXXXXXXXXXXXXX"
              }
          },
          "value": "female",
          "formattedValue": "Female"
      }
  ],
  "birthdays": [
      {
          "metadata": {
              "primary": true,
              "source": {
                  "type": "PROFILE",
                  "id": "1XXXXXXXXXXXXXXXXXXXXXXXX"
              }
          },
          "date": {
              "month": 8,
              "day": 9
          }
      },
      {
          "metadata": {
              "source": {
                  "type": "ACCOUNT",
                  "id": "XXXXXXXXXXXXXXXXXXXXXXx"
              }
          },
          "date": {
              "year": 1996,
              "month": 8,
              "day": 9
          }
      }
  ],
  "ageRanges": [
      {
          "metadata": {
              "primary": true,
              "source": {
                  "type": "PROFILE",
                  "id": "XXXXXXXXXXXXXXXXXXXXXXXXX"
              }
          },
          "ageRange": "TWENTY_ONE_OR_OLDER"
      }
  ]
}

如您所见,缺少地址字段。


注意:在用户单击登录按钮后,我正在从前端检索idToken和:accessToken

import GoogleLogin from "react-google-login";

export class LoginPage extends Component {
  onGoogleSignInSucceeded(response) {
    const { accessToken, tokenId } = response;
  }
  render() {
    const { errors } = this.state;
    return (
      <>
        <GoogleLogin
          clientId="XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"
          scope="https://www.googleapis.com/auth/user.birthday.read https://www.googleapis.com/auth/user.gender.read https://www.googleapis.com/auth/user.addresses.read"
          // IMPORTANT
          // https://developers.google.com/people/api/rest/v1/people/get#authorization-scopes
          buttonText="Sign-in with your Google account"
          onSuccess={this.onGoogleSignInSucceeded}
          onFailure={this.onGoogleSignInFailed}
          cookiePolicy={"single_host_origin"}
        />
      </>
    );
  }
}

标签: javascriptoauthgoogle-signingoogle-authenticationgoogle-people-api

解决方案


推荐阅读