Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

javascript - Google People Api: Adresses field is returned "undefined"

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

All the fields request here:

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

are returned correctly except for the addresses field which is logged as undefined.
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
          }
      }
  ],
  "ageRanges": [
      {
          "metadata": {
              "primary": true,
              "source": {
                  "type": "PROFILE",
                  "id": "XXXXXXXXXXXXXXXXXXXXXXXXX"
              }
          },
          "ageRange": "TWENTY_ONE_OR_OLDER"
      }
  ]
}

As you see the addresses field is missing.


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"}
        />
      </>
    );
  }
}
question from:https://stackoverflow.com/questions/65919367/google-people-api-adresses-field-is-returned-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...