Google People Api: Country not returned in Adresses field

[亡魂溺海] 提交于 2021-02-11 12:41:17

问题


I am trying to get the authenticated user's country by specifying "addresses" field in the People Api request as specified here. This is the code:

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,addresses",
    });

    const { birthdays, genders, addresses, ageRanges } = data;
    return data;
  } catch (error) {
    console.log("error: ", error);
    throw new Error("Google token validation failed");
  }
};

I have set up the addresses information in the account I am using to sign-in like this:

The is the whole response I get after sending the request:

{
  "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
          }
      }
  ],
  "addresses": [
    {
        "metadata": {
            "primary": true,
            "source": {
                "type": "PROFILE",
                "id": "111110367350060808978"
            }
        },
        "formattedValue": "XXXXXXXXXXXXX, XXXXX, XXXXX",
        "type": "home",
        "formattedType": "Home"
    }
],
}

As you see the country field is missing in "addresses.


NOTE: I am retrieving the idToken and the accessToken from the FrontEnd, after the user clicks on the SignIn button:

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"}
        />
      </>
    );
  }
}

回答1:


All fields on the address field are optional (see docs). This also includes the country. There is also no guarantee that the data is actually correct (the user can add invalid data to their google profile), so be careful about that and check for the verified metadata (see docs).

That being said you could try using a geocoding API to get the country from the formatted address. This could be made by using a reverse geocoding query (Google Geocoding API documentation).

Also notice that there are other fields that may contain an address. For example locations (see docs) can contain information about where they live.



来源:https://stackoverflow.com/questions/65934190/google-people-api-country-not-returned-in-adresses-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!