25

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,035
  • 2
  • 24
  • 41
  • 4
    This API cannot be used in a content script. – Rob W Oct 16 '14 at 08:51
  • 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
  • 9
    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

2 Answers2

43

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,077
  • 2
  • 28
  • 42
Bonton255
  • 2,035
  • 2
  • 24
  • 41
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
  • 23,662
  • 12
  • 81
  • 137