user2693135 user2693135 - 9 months ago 59
Javascript Question

AJAX GMAIL API Call Authorization error

I'm new to the GMail API and am trying to make an AJAX call to retrieve mails.

My code is:

beforeSend: function (request)
request.setRequestHeader("authorization", "Bearer");
url: '',
dataType: 'json',
cache: false,
success: function(data) {
// this.setState({Emails: data});
console.log("Mail thread"+data);
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());

The response is a 401 error. On examining the request, I find the following query parameter appended to the request URL getting sent:


So the request URL appears like this.

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.


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...';

  url: '' + accessToken,
  dataType: 'json',
  success: function(data) {