Reputation: 3802
I wanted to retrieve the email address of the contacts for a user. I am planning to use Google People API. Here is my function to retrieve:
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);
});
}
Basically, I am fetching 200 connections to retrieve their resourceName.
This reresourceName will be used to get the email address of the user.
I am using a batch request to fetch the 200 users email.
The issue is that, for some users, I am getting the following error:
{
"error": {
"code": 429,
"message": "Insufficient tokens for quota group and limit ReadGroupUSER-100s using the limit by ID 991457241470@515501272068.",
"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"
}
]
}
]
}
}
What could be the reason and how can I resolve this issue?
Upvotes: 2
Views: 2930
Reputation: 3802
Finally, I got the solution. It uses Google API to login and an AJAX call to fetch the contact details.
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");
}
});
}
Upvotes: 2