chrome.identity

Description: Use the chrome.identity API to get OAuth2 access tokens.
Availability: Stable since Chrome 29.
Permissions: "identity"
Learn More: Identify User

Summary

Methods
getAuthToken chrome.identity.getAuthToken(object details, function callback)
removeCachedAuthToken chrome.identity.removeCachedAuthToken(object details, function callback)
launchWebAuthFlow chrome.identity.launchWebAuthFlow(object details, function callback)
getRedirectURL string chrome.identity.getRedirectURL(string path)
Events
onSignInChanged

Methods

getAuthToken

chrome.identity.getAuthToken(object details, function callback)

Gets an OAuth2 access token using the client ID and scopes specified in the oauth2 section of manifest.json.

The Identity API caches access tokens in memory, so it's ok to call getAuthToken any time a token is required. The token cache automatically handles expiration.

Parameters
object (optional) details Token options.
boolean (optional) interactive Fetching a token may require the user to sign-in to Chrome, or approve the application's requested scopes. If the interactive flag is true, getAuthToken will prompt the user as necessary. When the flag is false or omitted, getAuthToken will return failure any time a prompt would be required.
function callback Called with an OAuth2 access token as specified by the manifest, or undefined if there was an error.

The callback parameter should be a function that looks like this:

function(string token) {...};
string (optional) token

removeCachedAuthToken

chrome.identity.removeCachedAuthToken(object details, function callback)

Removes an OAuth2 access token from the Identity API's token cache.

If an access token is discovered to be invalid, it should be passed to removeCachedAuthToken to remove it from the cache. The app may then retrieve a fresh token with getAuthToken.

Parameters
object details Token information.
string token The specific token that should be removed from the cache.
function callback Called when the token has been removed from the cache.

The callback parameter should be a function that looks like this:

function() {...};

launchWebAuthFlow

chrome.identity.launchWebAuthFlow(object details, function callback)

Starts an auth flow at the specified URL.

This method enables auth flows with non-Google identity providers by launching a web view and navigating it to the first URL in the provider's auth flow. When the provider redirects to a URL matching the pattern https://<app-id>.chromiumapp.org/*, the window will close, and the final redirect URL will be passed to the callback function.

Parameters
object details WebAuth flow options.
string url The URL that initiates the auth flow.
boolean (optional) interactive Whether to launch auth flow in interactive mode.

Since some auth flows may immediately redirect to a result URL, launchWebAuthFlow hides its web view until the first navigation either redirects to the final URL, or finishes loading a page meant to be displayed.

If the interactive flag is true, the window will be displayed when a page load completes. If the flag is false or omitted, launchWebAuthFlow will return with an error if the initial navigation does not complete the flow.
function callback Called with the URL redirected back to your application.

The callback parameter should be a function that looks like this:

function(string responseUrl) {...};
string (optional) responseUrl

getRedirectURL

string chrome.identity.getRedirectURL(string path)

Generates a redirect URL to be used in |launchWebAuthFlow|.

The generated URLs match the pattern https://<app-id>.chromiumapp.org/*.

Parameters
string (optional) path The path appended to the end of the generated URL.

Events

onSignInChanged

Fired when signin state changes for an account on the user's profile.

addListener

chrome.identity.onSignInChanged.addListener(function callback)
Parameters
function callback

The callback parameter should be a function that looks like this:

function(object account, boolean signedIn) {...};
object account
string id A unique identifier for the account. This ID will not change for the lifetime of the account.
boolean signedIn