Dan Klos
Dan Klos

Reputation: 709

Using Chrome auth to access gmail api inside of a Chrome Extension

Building a Chrome Extension for Gmail, trying to use Chrome Auth to gain access to gmail api per this StackOverflow post and the Gmail API docs, among others. I successfully receive the token with chrome.identity.getAuthToken({ 'interactive': true }, function(token) {} however when I plug the token into the request url I get the following 401 error response (code follows)

error response

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "required",
        "message": "Login Required",
        "locationType": "header",
        "location": "Authorization"
      }
    ],
    "code": 401,
    "message": "Login Required"
  }
}

My code:
background.js

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);
        }
      );
    });
  }
})

manifest.json

{
  "manifest_version": 2,
  "key": "<key>",
  "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": "<client id>",
    "scopes": ["https://www.googleapis.com/auth/gmail.modify"]
  }
}

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.

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.

Upvotes: 3

Views: 1729

Answers (1)

abraham
abraham

Reputation: 47893

key is for generic application secrets. For user specific tokens you need to use access_token. And token should not be wrapped in {}. If mail%40gmail.com is the actual value you are using in the URL, it won't work. It either needs to be the email address of the authenticated user or me.

So change:

"https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"

To this:

"https://www.googleapis.com/gmail/v1/users/me/threads?access_token=" + thisToken

Upvotes: 2

Related Questions