I'm trying to test out the Google Contacts API with JavaScript. I know that V1 had support for JavaScript but V2 and V3 don't list it. However, I have been able to find a way to retrieve contacts through an AJAX request so getting the contacts isn't really the problem right now. However, I'd like to be able to specify the search criteria of the contact that I'm looking for so that I don't have to page and search through all of the users contacts.
Right now, my code looks like this:
function getContact(domTarget) {
// Get permission and a token
auth(function(token) {
// Create an AJAX request to get the contacts
var ajaxContactRequest = new XMLHttpRequest(),
url = "";
// What to do when we get our contacts
ajaxContactRequest.onreadystatechange = function() {
if (ajaxContactRequest.readyState === 4 && ajaxContactRequest.status === 200) {
// Parse our contact data as a JSON object
var response = JSON.parse(ajaxContactRequest.responseText),
contactArray = response.feed.entry, i, contactDiv;
// Print out the contacts
if (domTarget) {
for (i = 0; i < contactArray.length; i++) {
if (contactArray[i].title && contactArray[i].title.$t) {
contactDiv = document.createElement("div");
domTarget.appendChild(contactDiv);
// Print out the contact's name
contactDiv.innerHTML = contactArray[i].title.$t;
}
}
}
}
};
// Construct our URL
url += "https://www.google.com/m8/feeds/contacts/default/full/";
// Add the access token
url += "?access_token=" + token.access_token;
// Get JSON format back
url += "&alt=json";
// Limit to 10 results
url += "&max-results=10";
// Open the request
ajaxContactRequest.open("GET", url, false);
// Send it away
ajaxContactRequest.send();
});
}
I know that there is some sort of support for queries because the listing here: Retrieving Contacts Using Query Parameters mentions that you can use the parameters found here: Contacts Query Parameters Reference which lists q=term1 term2 term 3
and `q="term1 term2 term3" are the way to do "Fulltext query on contacts data fields".
I have tried several different mixes of parameters such as first and last name as well as dates and emails. However, none of them have any impact on the results. Another thing that I am wondering is if this really matters. I'm limiting my query based on name only to try and reduce the size of the response. However, if the better practice is to just grab the whole contact set and then glean the bits of info that you need, I guess I can do that instead.
I figure you have to do multiple requests if the number of contacts is larger than the max-results
size.