I'm assuming your problem is to fetch a valid API key (because it was the tricky part for me). If you have other problem, just comment below and I'll give a more detailed example.
I managed to do it using the Identity API, documented here. With it, you can get identity data from the user logged in the browser, including a token. All I needed to do was:
//manifest.json
//...
"permissions": [
"identity",
//...
],
//...
"key": "__Really_long_gibberish_token_string__",
"oauth2": {
"client_id": "__some_token_string__.apps.googleusercontent.com",
"scopes": [ "https://www.googleapis.com/auth/spreadsheets.readonly" ]
}
Everything is linked to a google project, so, details about key and clientId generation can be found here.
And created a function to retrieve the token:
// This function is inside a component which ends up bundled in my worker.js; I didn't tried it in content scripts, nor popups.
async function fetchGoogleToken() {
const tokenObj = await chrome.identity.getAuthToken({interactive: true})
return tokenObj.token
}
The interactive: true
option is wonderful. If the user isn't logged in the browser, it will show a login form and authorization permission confirmation window for you.
This is the token I sent to the API.