使用Javascript中的Google People API获取联系人电子邮件地址
问题描述:
我想检索用户的联系人的电子邮件地址。 我打算使用Google People API。 这里是我的函数来检索:使用Javascript中的Google People API获取联系人电子邮件地址
function makeApiCall() {
gapi.client.load('people', 'v1').then(function() {
var request = gapi.client.people.people.connections.list({
resourceName: 'people/me',
pageSize: 200,
fields: 'connections(resourceName)'
});
request.execute(function(resp) {
if (resp.connections){
var batch = gapi.client.newBatch();
var emails = [];
for(var i = 0; i < resp.connections.length; i++){
var req = gapi.client.people.people.get({
resourceName: resp.connections[i].resourceName,
fields: "emailAddresses(value)"
});
batch.add(req);
req.then(function (email){
var idx = email.body.indexOf("{");
var jsonString = email.body.substring(idx > 0 ? idx : 0);
var obj;
try {
obj = JSON.parse(jsonString);
if (obj.emailAddresses){
for (j = 0; j < obj.emailAddresses.length; j++){
emails.push(obj.emailAddresses[j].value);
}
}
} catch (err) {
console.error(email);
}
})
}
batch.then(function(){
console.log(emails);
}, function (err){
console.error(err);
});
} else {
console.error("Error", resp);
}
});
}, function (err){
console.error(err);
});
}
基本上,我取200个连接检索他们的资源名称。
此reresourceName将用于获取用户的电子邮件地址。
我正在使用批量请求来获取200个用户的电子邮件。
的问题是,对于一些用户来说,我收到以下错误:
{
"error": {
"code": 429,
"message": "Insufficient tokens for quota group and limit ReadGroupUSER-100s using the limit by ID [email protected]",
"status": "RESOURCE_EXHAUSTED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/991457241470/apiui/credential"
}
]
}
]
}
}
可能是什么原因,我怎么能解决这个问题呢?
谢谢
答
最后,我得到了解决方案。它使用Google API登录并通过AJAX调用获取联系人详细信息。
function getEmailList($scope, callback, fetchBeginCallback) {
_googleLogin(AppConstants.GOOGLE_API.scopes.contactEmailListing, function(err) {
if (err) {
$scope.$apply(function() {
callback(err);
});
} else {
fetchBeginCallback ? fetchBeginCallback() : $.noop();
var emails = [];
$.ajax({
url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + gapi.auth.getToken().access_token + "&alt=json&max-results=1000",
dataType: 'jsonp'
}).done(function(data) {
var entries = data.feed.entry;
for (var i = 0; i < entries.length; i++) {
if (entries[i].gd$email) {
if (emails.indexOf(entries[i].gd$email[0].address) < 0) {
emails.push(entries[i].gd$email[0].address);
}
}
}
callback(undefined, emails);
}).fail(function(jqXHR, textStatus, errorThrown) {
callback(errorThrown, emails);
});
}
});
}
function _googleLogin(scope, callback) {
gapi.auth.authorize({
client_id: AppConstants.GOOGLE_API.clientId,
scope: scope,
immediate: false
}, function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
callback();
} else {
callback(authResult ? authResult.error : "Empty Response");
}
});
}
错误消息来自您的客户在100秒内可以完成的请求数量。如果你像我一样,你可能已经创建了一个循环,意外地反复钻取服务器。抱歉,我无法帮助解决您的核心问题。 –
我有这个相同的问题。根据https://console.developers.google.com/apis/api/people.googleapis.com/quotas,限制为每100秒17000个请求...我无法超越这个限制,所以我不确定发生了什么事:/ –
你解决了这个问题吗? –