Reputation: 1316
I'm new to the GMail API and am trying to make an AJAX call to retrieve mails.
My code is:
$.ajax({
beforeSend: function (request)
{
request.setRequestHeader("authorization", "Bearer xxxxxxxxxxxxxxxxx.apps.googleusercontent.com");
},
url: 'https://www.googleapis.com/gmail/v1/users/me/messages?key=xxxxxxxxxxxxxxxxxx',
dataType: 'json',
cache: false,
success: function(data) {
// this.setState({Emails: data});
console.log("Mail thread"+data);
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
The response is a 401 error. On examining the request, I find the following query parameter appended to the request URL getting sent:
&_=1470236511985
So the request URL appears like this.
https://www.googleapis.com/gmail/v1/users/me/messages?key=xxxxxxxxxxxxxxxxxx&_=1470236511985
Is the appended query parameter causing the 401 error or am I using the authorization header incorrectly? How can I resolve this.
Thanks in advance.
Upvotes: 1
Views: 339
Reputation: 112807
For testing purposes, you can go to the OAuth 2.0 Playground and get an access token with the Gmail API scopes. Just use this access token in a query parameter named access_token
:
var accessToken = 'ya29...';
$.ajax({
url: 'https://www.googleapis.com/gmail/v1/users/me/messages?access_token=' + accessToken,
dataType: 'json',
success: function(data) {
console.log(data.messages);
}
}
Upvotes: 1