我需要通过Google People API获取联系人电子邮件,但无法找到在文档中执行此操作的方法。目前我正在提出以下要求:
request.get('https://people.googleapis.com/v1/people/me/connections?access_token=tokenHere',
function (error, response, body) {
console.log(body);
});
并获得以下响应(例如,我只将其中的一部分固定):
{
"resourceName": "people/c1705421824339784415",
"etag": "328OLZwdaiQ=",
"metadata": {
"sources": [
{
"type": "CONTACT",
"id": "17aae01d0ff8b2df",
"etag": "#328OLZwdaiQ="
}
],
"objectType": "PERSON"
},
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "17aae01d0ff8b2df"
}
},
"displayName": "testGoogleContact",
"givenName": "testGoogleContact",
"displayNameLastFirst": "testGoogleContact"
}
]
}
答案 0 :(得分:3)
要实现此目的,您需要使用Google Plus API:这是我在Google Plus API文档页面上找到的内容:
您可以使用以下命令获取经过身份验证的用户的电子邮件地址 电子邮件范围。
以下JavaScript代码示例演示如何:
使用Google+登录功能对用户进行身份验证并获取有效的OAuth 2.0 访问令牌。
使用令牌向
发出HTTP GET请求
https://www.googleapis.com/plus/v1/people/me
REST端点。解析响应并显示用户的电子邮件 地址。
JSON应该是这样的:
{"kind":"plus#person","etag":"\"xw0en60W6-NurXn4VBU-CMjSPEw/mjjYoraGfq3Wi-8Nee4F3k7GYrs\"","emails":[{"value":"**EMAIL**","type":"account"}],"objectType":"person","id":"Person ID","displayName":"FULL NAME","name":{"familyName":"LAST NAME","givenName":"NAME"},"url":"https://plus.google.com/USER","image":{"url":"https://lh5.googleusercontent.com/-RTcRn6jTuoI/AAAAAAAAAAI/AAAAAAAAEpg/Y6cMxfwtbQ4/photo.jpg?sz=50","isDefault":false},"placesLived":[{"value":"CITY","primary":true}],"isPlusUser":true,"verified":false,"cover":{"layout":"banner","coverPhoto":{"url":"https://lh3.googleusercontent.com/SybH-BjYW2ft1rzayamGLg_VwW7ocgnQ5cAxH3ROEpODvyaEODpYKW55gmAxCXDUvfKggQ4=s630-fcrop64=1,00002778ffffffff","height":626,"width":940},"coverInfo":{"topImageOffset":0,"leftImageOffset":0}},"result":{"kind":"plus#person","etag":"\"xw0en60W6-NurXn4VBU-CMjSPEw/mjjYoraGfq3Wi-8Nee4F3k7GYrs\"","emails":[{"value":"**EMAIL HERE**","type":"account"}],"objectType":"person","id":"116508277095473789406","displayName":"FULL NAME","name":{"familyName":"LAST NAME","givenName":"NAME"},"url":"https://plus.google.com/USER","image":{"url":"https://lh5.googleusercontent.com/-RTcRn6jTuoI/AAAAAAAAAAI/AAAAAAAAEpg/Y6cMxfwtbQ4/photo.jpg?sz=50","isDefault":false},"placesLived":[{"value":"CITY I LIVE","primary":true}],"isPlusUser":true,"verified":false,"cover":{"layout":"banner","coverPhoto":{"url":"https://lh3.googleusercontent.com/SybH-BjYW2ft1rzayamGLg_VwW7ocgnQ5cAxH3ROEpODvyaEODpYKW55gmAxCXDUvfKggQ4=s630-fcrop64=1,00002778ffffffff","height":626,"width":940},"coverInfo":{"topImageOffset":0,"leftImageOffset":0}}}}
答案 1 :(得分:2)
如果有其他人遇到这个问题:检索授权用户的电子邮件地址的解决方案,OP似乎想要的是什么(不是授权用户'自己的电子邮件地址)位于Why can't I retrieve emails addresses and phone numbers with Google People API?。
解释:如果您查看"查询参数"下的部分。在https://developers.google.com/people/api/rest/v1/people.connections/list上,您会看到requestMask
是一个参数(记录在https://developers.google.com/people/api/rest/v1/RequestMask)。
它表示您需要在查询中包含requestMask
参数,因为您正在进行people.list
查询(即使用connections
GET端点)。 requestMask
参数基本上告诉API要提取哪些字段:person.emailAddresses
告诉它拉动各个人群'电子邮件地址,person.emailAddresses,person.names
告诉它提取电子邮件地址和姓名等。
答案 2 :(得分:2)
获取https://people.googleapis.com/v1/people/me/connections?sortOrder=FIRST_NAME_ASCENDING&fields=connections(emailAddresses%2Cnames)&key= {YOUR_API_KEY}
试试这个,它会给你发电子邮件。确保您的联系人有电子邮件。
答案 3 :(得分:1)
您可以向https://people.googleapis.com/v1/people/me
发出请求,从Google People API获取个人资料电子邮件。
如果您需要非公开电子邮件,则需要https://developers.google.com/people/v1/how-tos/authorizing#profile-scopes
中指定的email
或https://www.googleapis.com/auth/user.emails.read
范围