someone
someone

Reputation: 691

How to use access token from google calendar in js

I have question regarding google calendar, and hope you can help me.

I have access_token from google calendar and that access_token was stored in localStorage.

          const googleAccessToken = e.vc.access_token;
          localStorage.setItem('googleAccessToken', googleAccessToken);

Now please help me understand how can I use this to avoid authorization every time.

For example I want to delete all events, And for that I wrote this code, and this code every time wants from me authorization:

   const handleDisconnect = () => {
    gapi.load('client:auth2', () => {
      console.log('loaded client');

      gapi.client.init({
        apiKey: API_KEY,
        clientId: CLIENT_ID,
        discoveryDocs: DISCOVERY_DOCS,
        scope: SCOPES_CLEAR,
      });
      gapi.client.load('calendar', 'v3', () => {
        console.log('sdsd');
      });
      gapi.auth2
        .getAuthInstance()
        .signIn()
        .then(() => {
          var events = bb;
          var makeRequest = resource => {
            console.log(resource);

            var request = gapi.client.calendar.calendars.clear({
              calendarId: 'primary',
            });

            request.execute(resp => {
              console.log(resp);
            });
          };

          for (var j = 0; j < events.length; j++) {
            makeRequest(events[j]);
          }
        });
    });
    };

Please help me to fixed that code with access_token for avoiding authorization

Upvotes: 1

Views: 584

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116968

Access tokens expire after one hour. Once the access token has expired you will need to authorize the user again order to have access to their data again.

Normally with Oauth2 you can request something called off line access this will then give you something called a refresh token. Refresh tokens for applications that are in production are long lived and for the most part do not expire. You can use a refresh token to request an new access token when ever needed. This way you will not need to authorize the user after an hour.

Unfortunately refresh tokens are not supported by client sided authorization using JavaScript. You will need to switch to a server sided language.

If you are trying to access a static calendar account owned by you the developer as opposed to calendar accounts owned by your users you could use something called service accounts. However service accounts only work with Google calendar if you use domain wide delegation to a google workspace account it will not work with standard Gmail calendar accounts. Also service accounts are not supported by JavaScript you will need to switch to a server sided programming language.

After all that I hope you can see if you are sticking with JavaScript you will need to request authorization of your users when ever the access token expires.

Upvotes: 1

Related Questions