Google 人 Api:地址字段中未返回国家/地区
Google People Api: Country not returned in Adresses field
我正在尝试通过指定 Api 请求中指定的“地址”字段来获取经过身份验证的用户的 国家/地区 here。
这是代码:
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");
}
};
我在用于登录的帐户中设置了 addresses information:
这是我发送请求后得到的完整响应:
{
"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"
}
],
}
如您所见,“地址”中缺少国家/地区字段。
注意: 在用户单击“登录”按钮后,我正在从前端检索 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"}
/>
</>
);
}
}
地址字段中的所有字段都是可选的 (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)。
话虽如此,您可以尝试使用地理编码 API 从格式化的地址中获取国家/地区。这可以通过使用反向地理编码查询 (Google Geocoding API documentation) 来实现。
另请注意,还有其他字段可能包含地址。例如 locations
(see docs) 可以包含关于他们住在哪里的信息。
我正在尝试通过指定 Api 请求中指定的“地址”字段来获取经过身份验证的用户的 国家/地区 here。 这是代码:
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");
}
};
我在用于登录的帐户中设置了 addresses information:
这是我发送请求后得到的完整响应:
{
"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"
}
],
}
如您所见,“地址”中缺少国家/地区字段。
注意: 在用户单击“登录”按钮后,我正在从前端检索 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"}
/>
</>
);
}
}
地址字段中的所有字段都是可选的 (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)。
话虽如此,您可以尝试使用地理编码 API 从格式化的地址中获取国家/地区。这可以通过使用反向地理编码查询 (Google Geocoding API documentation) 来实现。
另请注意,还有其他字段可能包含地址。例如 locations
(see docs) 可以包含关于他们住在哪里的信息。