29

I am writing a chrome extension in which I am trying to use the chrome.identity API. But my Chrome doesn't recognize identity.

On the following code in developer tools, I get an error saying "Cannot read property getAuthToken of undefined:

chrome.identity.getAuthToken({ 'interactive': false }, function(token) {

I tried typing in the console. chrome.extension works but chrome.identity is undefined.

My manifest.json has "identity" in permissions. I am on latest Chrome v38. Is there anything else required to enable the identity API?

Bonton255
  • 2,231
  • 3
  • 28
  • 44
  • 4
    This API cannot be used in a content script. – Rob W Oct 16 '14 at 08:51
  • 1
    The identity sample (https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/identity) uses it in a js file in the extension, same as me. I am sure I am missing something. – Bonton255 Oct 16 '14 at 09:03
  • 12
    The API can be used in a background page, extensions page, options page or popup page, but not in a content script. – Rob W Oct 16 '14 at 09:04
  • 1
    Thanks Rob, that was the information I was looking for. Really helpful! – Bonton255 Oct 16 '14 at 14:03

3 Answers3

51

The reason I was not able to use identity was because I was trying to access it from a content script. I switched to a background script and it works now! Thanks Rob!

PS! You also need to have "permissions": ["identity"] set in your manifest.json.

kano
  • 5,626
  • 3
  • 33
  • 48
Bonton255
  • 2,231
  • 3
  • 28
  • 44
  • 4
    I am trying to do it in background.js but its not working – Tahir Yasin Oct 17 '15 at 17:44
  • 3
    I spent a couple hours struggling on the same thing. It's amazing this isn't included in the docs. – arhoskins Feb 02 '16 at 05:07
  • 17
    @TahirYasin I had this issue too and it turns out I forgot to add `"permissions": ["identity"]` to my manifest.json. – adrianmcli Aug 10 '16 at 16:41
  • @adrianmc this should be included in the accepted answer! Most likely culprint – Arthur Feb 17 '17 at 02:14
  • 2
    how to make content.js invoke the getAuth within background.js? – gumuruh Sep 16 '19 at 09:49
  • You *can* use chrome identity from a content script. Just setting the permissions as described in the answer does the trick. – Martin Staufcik Dec 21 '22 at 12:27
  • Code would have been helpful. I had "background": { "service_worker": "background.js" }, and google documentation asks to create new file called oauth.js as here: https://developer.chrome.com/docs/extensions/mv3/tut_oauth/#oauth_client So how to add another oauth.js in background script? – Sudhir Kaushik Jun 05 '23 at 18:10
2

It may require providing a "key" value in your manifest as well (if you're trying to get it working locally and it's not working). You can either use the same key as the one you get when you upload your extension to the webstore or try packing an extension to generate a new one (though I couldn't get this second approach working myself).

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
2

You can access it in your content script, ie content.js by using the 'Messaging API" to send a message to your background.js and return it to the content.js.

content.js:

chrome.runtime.sendMessage({type: "getAuthToken"}, function(response) {
  alert(response.token);
});

and in background.js:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.type === "getAuthToken") {
    chrome.identity.getAuthToken({interactive: true}, function(token) {
      sendResponse({token: token});
    });
    return true;
  }
});

and your manifest.json should at minimum have these properties:

{
  ...
  "permissions": [
    ...
    "identity",
    ...
  ],
  ...
  "background": {
    "service_worker": "background.js"
  },
  ...
  "content_scripts": [
    ...
    {
      "matches": ["https://example.com/*"],
      "js": ["content.js"],
      "match_origin_as_fallback": false
    },
    ...
  ]
}

Now when you go to example.com, an alert should appear with the logged in users token, assuming your extension has already logged in the user.

mchavezi
  • 482
  • 1
  • 4
  • 14