Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhuqling/aef0170e413a668b63d37aaa0f8343d4 to your computer and use it in GitHub Desktop.
Save zhuqling/aef0170e413a668b63d37aaa0f8343d4 to your computer and use it in GitHub Desktop.
How to set up user authentication for a Chrome Extension using the Chrome Identity API

How to set up user authentication for a Chrome Extension using the Chrome Identity API

  1. Create a private key file, from which you can create the manifest key and Application ID, as detailed here: https://stackoverflow.com/questions/23873623/obtaining-chrome-extension-id-for-development
  2. Add the manifest key to "key" in manifest.json
  3. Create a new project in Google Developer Console https://console.developers.google.com/project
  4. Go to "APIs & auth > Credentials" and create new client id for a Chrome Application using the Application ID generated in step 3.
  5. Copy the Client ID to oauth2.client_id in the manifest.json

Deprecated?

  1. Create a new app on the Chrome Web Store Developer Dashboard https://chrome.google.com/webstore/developer/dashboard
  2. Click on "more info" and copy the public key to "key" in manifest.json. (or not needed because of next step...?)

Resources

chrome.identity.getAuthToken({
interactive: true
}, function(token) {
if (chrome.runtime.lastError) {
alert(chrome.runtime.lastError.message);
return;
}
var x = new XMLHttpRequest();
x.open('GET', 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + token);
x.onload = function() {
alert(x.response);
};
x.send();
});
{
"key": "<Application ID>",
"name": "Identity test",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": [
"identity"
],
"oauth2": {
"client_id": "<Client ID>",
"scopes": [
"https://www.googleapis.com/auth/userinfo.email"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment