Before a Chrome App can access Google APIs, you must register it with Google. Registration is documented by Google (read the “User Authentication” section), but as of October 2014, the Google Developers Console user interface has been updated, and the step-by-step instructions are out whack with what the website does. What follows here works as of this writing, but be warned that it might change again by the time you read it.
To register your Chrome App and obtain a client ID, you need to do the following:
Copy the client ID and update your manifest with OAuth2 client ID and scopes:
{ "app": { "background": { "scripts": [ "background.js" ] } }, "manifest_version": 2, "name": "GDrive", "version": "1.0.0", "permissions": [ "identity" ], "key": "MIIBIjANB...yyltEwIDAQAB", "oauth2": { "client_id": "2546277...3kj.apps.googleusercontent.com", "scopes": [ "https://www.googleapis.com/auth/drive" ] } }
On the first call to chrome.identity.getAuthToken
(see Accessing Google APIs), you are asked to confirm the permissions, as shown in Figure B-5.
Generally, while you’re developing a Chrome App you’ll load it in unpacked form by clicking the “Load unpacked extension” button on the Extensions page, as described in Chapter 1. That causes an app ID to be assigned to the app, but then if you install the app, either by dropping a .crx
file onto the Extensions page or downloading it from the Chrome Web Store, a different app ID will be assigned. If you go back to developing the app as unpacked, you might get the earlier app ID, or perhaps even a different one.
Usually you don’t care what the app ID is, but when you register an app, you need to supply a specific app ID, and it’s annoying if it changes because you have to keep updating the app’s registration and getting a new client ID. It’s much easier to fix the app ID so that it never changes during development.
Here’s what you do:
.crx
file to the Extensions page. Verify that the app is installed and runs by clicking its “launch” link on the Extensions page. (It may no longer be at the top of the page, so scroll down until you find it.)
key
property from the installed manifest and paste it into the manifest.json file in your development source. With the key
in place there, Chrome will always use the same app ID, whether the app is loaded as unpacked or installed. You can then use that app ID when you register the app, and you no longer need to worry about it changing. Accordingly, the client ID will be permanently fixed, as well. (You will have to remove the key
property before you upload the app to the Chrome Web Store, and you may get a different app ID then.)