I want to authenticate gapi client library so I could create a spreadsheet on behalf of another user, i.e. oauth.
This code used to work:
gapi.load('client:auth2', function() {
gapi.auth2.init({
clientId: 'CLIENT_ID',
scope: 'https://www.googleapis.com/auth/spreadsheets',
}).then(function() {
return gapi.auth2.getAuthInstance().signIn();
}).then(function() {
console.log('Signed in!');
}, function(error) {
console.error(error);
});
});
Now however it is saying that gapi.auth2 is deprecated, so I try to do following
google.accounts.id.initialize({
client_id: 'CLIENT_ID',
callback: handleCredentialResponse
});
google.accounts.id.prompt();
function handleCredentialResponse(response) {
gapi.client.setToken(response.credential)
}
And then
function createSpreadsheet() {
gapi.client.sheets.spreadsheets
.create({
properties: {
title: "My New Spreadsheet",
},
})
.then((response) => {
// The spreadsheet is created successfully
console.log("Spreadsheet created:", response.result.spreadsheetUrl);
})
.catch((err) => {
console.error("Failed to create spreadsheet", err);
});
}
And this gives me "Missing credential error"
I've also tried setting credential like this:
gapi.auth.setToken({ access_token: token })
Same error.
What am I missing?
After some time I've figured it out and was able to authenticate gapi to use spreadsheets API.
I was confused by ID token and Authorization token, those are 2 different things. Without further due, here's the working example:
Note, that this is implicit authorization flow, that is less secure, but does not involve backend. Read more here: https://developers.google.com/identity/oauth2/web/guides/use-token-model