Reputation: 11
Following the code from: https://developers.google.com/calendar/api/quickstart/js
It states that "Authorization information is stored in the file system, so the next time you run the sample code, you aren't prompted for authorization."
However it currently needs me to request permission every time I reload the webpage.
Any ideas?
const CLIENT_ID = '';
const API_KEY = '';
const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest';
const SCOPES = 'https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar';
let tokenClient;
let gapiInited = false;
let gisInited = false;
function gapiLoaded() {
gapi.load('client', initializeGapiClient);
}
async function initializeGapiClient() {
await gapi.client.init({
apiKey: API_KEY,
discoveryDocs: [DISCOVERY_DOC],
});
gapiInited = true;
}
function gisLoaded() {
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: '', // defined later
});
gisInited = true;
}
//? Sign In
function handleAuthClick() {
tokenClient.callback = async (resp) => {
if (resp.error !== undefined) {
throw (resp);
}
};
if (gapi.client.getToken() === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
tokenClient.requestAccessToken({prompt: 'consent'});
} else {
// Skip display of account chooser and consent dialog for an existing session.
tokenClient.requestAccessToken({prompt: ''});
}
}
//? Sign Out
function handleSignoutClick() {
console.log("sign in")
const token = gapi.client.getToken();
if (token !== null) {
google.accounts.oauth2.revoke(token.access_token);
gapi.client.setToken('');
}
}
Upvotes: 1
Views: 69
Reputation: 117254
It is true that at the bottom of the JavaScript quick start for Google calendar it states.
Authorization information is stored in the file system, so the next time you run the sample code, you aren't prompted for authorization.
This is not true for the JavaScript sample as it is designed for a web app. This is an error in the sample and I have reported it to the team.
The node.js sample does however store it in the file system as its designed for an installed app. Im guessing someone copied the text and didnt fix it.
Upvotes: 0