0
votes

I want to get all the events in 1 month from google calendar API in flutter.

I consulted here but it has no instructions for flutter https://developers.google.com/calendar/v3/reference/events/get

I import https://pub.dev/packages/googleapis But I could not find any examples.

If you have done this or have any samples. Please share it for me.

1
@Doc thank you. But I want setup CalenderApi. final _credentials = new ServiceAccountCredentials.fromJson(). What is private_key_id, private_key?Nguyễn Trung Hiếu

1 Answers

1
votes

After 1 day of research. I made it as follows

Import libs

date_utils: ^0.1.0+3

googleapis_auth: ^0.2.11+1

googleapis: ^0.54.0

Code

final GoogleSignIn _googleSignIn =
        GoogleSignIn(
          scopes: <String>[
            CalendarApi.CalendarScope,
          ],
        )

signInWithGoogle() async {
   await _googleSignIn.signIn();
   final headers = await _googleSignIn.currentUser.authHeaders;
   getEvent(headers);
}


getEvent(headers) async {
final httpClient = GoogleHttpClient(headers);
var calendar = CalendarApi(httpClient);
DateTime lastMonth = Utils.lastDayOfMonth(DateTime.now());
DateTime firstMonth = Utils.firstDayOfMonth(DateTime.now());
var calEvents = calendar.events.list("primary", timeMax: lastMonth.toUtc(), timeMin: firstMonth.toUtc());
calEvents.then(
    (events) => {events.items.forEach((event) => print("EVENT ${event.summary}"))});
}

This is GoogleHttpClient.class

 import 'package:http/io_client.dart';
import 'package:http/http.dart';

class GoogleHttpClient extends IOClient {
Map<String, String> _headers;

 GoogleHttpClient(this._headers) : super();

 @override
 Future<StreamedResponse> send(BaseRequest request) =>
  super.send(request..headers.addAll(_headers));

 @override
 Future<Response> head(Object url, {Map<String, String> headers}) =>
  super.head(url, headers: headers..addAll(_headers));

 }

Hope it helpful