0

I have have run the examples from https://github.com/dart-lang/googleapis_examples/blob/master/drive_upload_download_console/bin/main.dart.

The example creates an authenticated HTTP client for accessing Google Drive API with:

import 'package:googleapis_auth/auth_io.dart' as auth;
…
…  

auth.clientViaUserConsent(identifier, scopes, userPrompt).then((client) {   // with client_id, client_secret, scope
  var api = new drive.DriveApi(client);
…
…
} 

When I run the example I have to give the User consent in a web browser each time I run the example above.

I want to create an authenticated HTTP client without having to use the user consent fonction (auth.clientViaUserConsent) but with a stored acces token or the refresh token. How can I create such an authenticated HTTP client? With the googleapis_auth package?(https://pub.dartlang.org/packages/googleapis_auth)

Jean G
  • 147
  • 1
  • 10
  • If your code is running on the server, you can save the Access Token and Refresh Token. You cannot do this if your code is running on the client. However, the user will need to authorize you at least once, your code cannot request privileged scopes (permissions) and your app will have to be approved by Google. Basically that means your token will be limited to the user's name and email address. – John Hanley Dec 12 '18 at 19:45
  • https://stackoverflow.com/questions/48477625/how-to-use-google-api-in-flutter/48485898#48485898 – Günter Zöchbauer Dec 12 '18 at 20:54

1 Answers1

1

You are already there.

Your client object contains all you need already.

Here is your modified code using the stored credentials to inquire about freeBusy time:

auth.clientViaUserConsent(identifier, scopes, userPrompt).then((client) {   // with client_id, client_secret, scope
  var api = new drive.DriveApi(client);
  debugPrint(' access token: ' + client.credentials.accessToken.data +' refresh token ' + client.credentials.refreshToken);
  // store the tokens in the apps key store 
}

At some time in the future make a new call to obtain new access credentials from the never expiring refresh token, and create a new client for your purposes.

    AccessCredentials _fromStorage = AccessCredentials(client.credentials.accessToken, 
    client.credentials.refreshToken, _scopes );

    var _newClient = new http.Client();
    AccessCredentials _accessCredentials = await refreshCredentials( _clientID, _fromStorage , _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());
    });
aknoefel
  • 123
  • 8