Google Calendar API - skip prompt after user gives consent

562

I discovered that the client contains all the information I need for future calls:

debugPrint(' access token: ' + client.credentials.accessToken.data +' refresh token ' + client.credentials.refreshToken);
// store the tokens in the apps key store

When my user makes a new call a second or a year from now, I retrieve the tokens from storage, create new credentials and make my calls:

AccessCredentials _newCredentials = AccessCredentials(fromStorage.credentials.accessToken, 
fromStrorage.credentials.refreshToken, _scopes );

var _newClient = new http.Client();
AccessCredentials _accessCredentials = await refreshCredentials( _clientID, newCredentials , _newClient);
_newClient = authenticatedClient(_newClient, _accessCredentials);
// the code below was just for me to test this out with my API scopes. replace with your code

var calendar = cal.CalendarApi(_newClient);
String calendarId = "---some string---";
cal.FreeBusyRequest _request = cal.FreeBusyRequest.fromJson(
    {
      'items': [
        {'id': calendarId, 'busy': 'Active'}
      ],
      'timeMin': (new DateTime(2020, 11, 17)).toIso8601String()+'Z',
      'timeMax': (new DateTime(2020, 11, 19)).toIso8601String()+'Z'
    });
  debugPrint('request: ' + _request.toJson().toString());
  cal.FreeBusyResponse response = await calendar.freebusy.query(_request);
  debugPrint(response.toJson().toString());
});
Share:
562
aknoefel
Author by

aknoefel

Not a trained software engineer, but have been working with code since the days of punch cards and 8bit register programming. Nowadays I do a lot of my prototypes in flutter, firebase and node.js.

Updated on December 25, 2022

Comments

  • aknoefel
    aknoefel over 1 year

    I have a flutter app that needs access to multiple user's calendars.

    I can get access for one user working, but the access grant is only temporary and the user gets re-prompted every time. I want to develop something like the firebase google authentication and many other Oauth examples: You get prompted once to grant access and then the access is granted offline until you revoke the access grant.

    How can I request access with the remembered credentials? Here is my code:

    final ClientId _userAccountCredentials = new ClientId(
          "XXXXX.apps.googleusercontent.com",
          "");
    
    void prompt(String url) async {
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    
      void getCalendarEventsOnline() {
        clientViaUserConsent(_userAccountCredentials, scopes, prompt).then((AuthClient client) {
          var calendar = calendarapi.CalendarApi(client);
          var calEvents = calendar.events.list("primary");
          calEvents.then((calendarapi.Events events) {
            /// some logic within
          });
          client.close();
        });
      }
    

    I tried using a service account, but impersonating a user works only for a Gsuite account

    Asking each user to add the service account to their calendar is error-prone, clumsy and unacceptable for non-technical users (think your grandparents), given that many popular services can do this directly