What is the Ajax call that I should make to get gmail contacts using JavaScript? I already have the user OAuth Token which I got because the user signed up to my site using Google.
What is the Ajax call that I should make to get gmail contacts using JavaScript? I already have the user OAuth Token which I got because the user signed up to my site using Google.
Share Improve this question edited Jan 26, 2015 at 12:12 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Jun 8, 2013 at 0:47 Darwish GaniDarwish Gani 111 silver badge2 bronze badges 2- Hi, wele to Stack Overflow. You might not have had quite right search terms, but see if this helps: developers.google./google-apps/contacts/v3 – Qantas 94 Heavy Commented Jun 8, 2013 at 2:31
- google./m8/feeds/contacts{userEmail}/full It seems that I need to make a GET request to this URL, however, I am confused as to how google knows I have a token that authorizes me to get these contacts. – Darwish Gani Commented Jun 8, 2013 at 5:47
2 Answers
Reset to default 5If you're using OAuth2 through JavaScript, you can use the Google Contacts API, but you'll need to get authorisation by sending the correct scope of permissions to Google when getting the access token, which is https://www.google./m8/feeds
. (reference)
As you already know how to get the access token, it's as simple as calling the API with the correct query. To get all contacts for your user, it's as simple as making an asynchronous request to the API for the required info. For example, where {userEmail}
is the user's email and {accessToken}
is your access token, simply make a GET
address to the following URI:
https://www.google./m8/feeds/contacts/{userEmail}/full?access_token={accessToken}&alt=json
A list of the types of queries you can send and their parameters are available here:
- Google Contacts API
- API Parameters
To get users' contacts using OAuth, first you need to specify the contact's scope in your request. If you're using ChromeExAuth, then you would write:
var oauth = ChromeExOAuth.initBackgroundPage({
'request_url' : 'https://www.google./accounts/OAuthGetRequestToken',
'authorize_url' : 'https://www.google./accounts/OAuthAuthorizeToken',
'access_url' : 'https://www.google./accounts/OAuthGetAccessToken',
'consumer_key' : 'anonymous',
'consumer_secret' : 'anonymous',
'scope' : 'https://www.googleapis./auth/userinfo.email https://www.googleapis./auth/userinfo.profile https://www.google./m8/feeds/',
'app_name' : 'MyApp'
});
The scope parameter above lists 3 scopes: the user's email, profile, and contacts (google./m8/feeds/contacts
)
To get their contacts after the user authorizes the token, you would send a request like this:
var url = "http://www.google./m8/feeds/contacts/default/full";
oauth.sendSignedRequest(url, onContacts, {
'parameters' : {
'alt' : 'json',
'max-results' : 99999
}
});
And the callback for the request could look like this:
function onContacts(text, xhr) {
contacts = [];
var data = JSON.parse(text);
for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
var contact = {
'name' : entry['title']['$t'],
'id' : entry['id']['$t'],
'emails' : []
};
if (entry['gd$email']) {
var emails = entry['gd$email'];
for (var j = 0, email; email = emails[j]; j++) {
contact['emails'].push(email['address']);
}
}
if (!contact['name']) {
contact['name'] = contact['emails'][0] || "<Unknown>";
}
contacts.push(contact);
}
};
To view the contacts array, you could just print on the console:
console.log(contacts);
You can checkout the Google OAuth tutorial here
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742314092a4420486.html
评论列表(0条)