title: Using Chrome auth to access gmail api inside of a Chrome Extension
body: <p>Building a Chrome Extension for Gmail, trying to use Chrome Auth to gain access to gmail api per <a href="https://stackoverflow.com/questions/25772391/gmail-api-access-from-chrome-extension-403-forbidden">this StackOverflow post</a> and <a href="https://developers.google.com/gmail/api/v1/reference/users/threads/list#try-it" rel="nofollow noreferrer">the Gmail API docs</a>, among others. I successfully receive the token with <code>chrome.identity.getAuthToken({ 'interactive': true }, function(token) {}</code> however when I plug the token into the request url I get the following 401 error response (code follows)</p>
  
  <p><strong>error response</strong></p>
  
  <pre><code>{
    "error": {
      "errors": [
        {
          "domain": "global",
          "reason": "required",
          "message": "Login Required",
          "locationType": "header",
          "location": "Authorization"
        }
      ],
      "code": 401,
      "message": "Login Required"
    }
  }
  </code></pre>
  
  <p>My code: <br>
  <strong>background.js</strong><br></p>
  
  <pre><code>chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
    if (changeInfo.status == 'complete') {
      chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
        thisToken = token
        chrome.runtime.onMessage.addListener(
          function(request,sender,sendResponse){
            var gapiRequestUrlAndToken = "https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"
  
            var makeGetRequest = function (gapiRequestURL)
              {
                  var xmlHttp = new XMLHttpRequest();
                  xmlHttp.open( "GET", gapiRequestURL, false );
                  xmlHttp.send( null );
                  return xmlHttp.responseText;
              }
  
            makeGetRequest(gapiRequestUrlAndToken);
          }
        );
      });
    }
  })
  </code></pre>
  
  <p><strong>manifest.json</strong></p>
  
  <pre><code>{
    "manifest_version": 2,
    "key": "&lt;key&gt;",
    "name": "exampleName",
    "description": "Description",
    "version": "0.0.1.7",
    "default locale": "en",
    "icons": { "128": "imgs/pledge_pin.png"},
    "content_scripts" : [
      {
        "matches": ["*://mail.google.com/mail/*"],
        "js": ["js/jquery.js", "js/compose.js", "bower_components/jqnotifybar/jquery.notifyBar.js"],
        "css": ["css/stylesheet.css", "bower_components/jqnotifybar/css/jquery.notifyBar.css"]
      }
    ],
    "background": {
      "scripts": ["scripts/background.js"]
    },
    "permissions": [
      "identity"
    ],
    "oauth2": {
      "client_id": "&lt;client id&gt;",
      "scopes": ["https://www.googleapis.com/auth/gmail.modify"]
    }
  }
  </code></pre>
  
  <p>I suspect it has something to do with the fact that I'm trying to use Chrome Auth for Gmail api, but other posts I've read have lead me to believe this is a viable option.</p>
  
  <p>In case my code didn't give it away, I'm a newbie, so any help is most welcome, and I greatly appreciate your time.</p>