I am trying to list all the events in my company calendar using Google Calendar Api.
I used the same code on this question: Using dart and flutter with google calendar api to get a list of events on the a user's calendar
And I followed all the 6 steps of the answer in the question above.
For some reason, the call clientViaServiceAccount do not return any result and any errors.
It seems like it is executing an infinity loop.
This is the code I am using, and I can only see printed "getCalendarEvents". I cannot see the msg "HERE" or any error printed. So The issue is for sure in clientViaServiceAccount.
I edited the code taking into account Iamblichus suggestion, but the issue is that I cannot even reach the CalendarApi. The code is stacked on the clientViaServiceAccount.
import 'package:flutter/material.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'package:googleapis/calendar/v3.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
CalendarAPI.getCalendarEvents();
return MaterialApp(
title: 'Flutter Demo',
home: Container(),
);
}
}
class CalendarAPI {
static final _accountCredentials = new ServiceAccountCredentials.fromJson(r'''
{
"private_key_id": myPrivatekeyId,
"private_key": myPrivateKey,
"client_email": myClientEmail,
"client_id": myClientId,
"type": "service_account"
}
''');
static final _scopes = [CalendarApi.CalendarScope];
static void getCalendarEvents() {
print('getCalendarEvents');
clientViaServiceAccount(_accountCredentials, _scopes).then((client) {
print('HERE');
var calendar = new CalendarApi(client);
print(calendar);
// Added iamblichus answer
var calendarListEntry = CalendarListEntry();
calendarListEntry.id = calendarId;
calendar.calendarList.insert(calendarListEntry).then((_) {
print('CALENDAR ADDED');
var calEvents = calendar.events.list(calendarId);
calEvents.then((Events events) {
events.items.forEach((Event event) {
print(event.summary);
});
}).catchError((e) => print(e));
}).catchError((e) => print(e));
}).catchError((e) => print(e));
}
}
EDIT FOR ANSWERING iamblichus COMMENT on Jul 15 at 12:31
I created the Service account Credentials on the page of the image shown below.
As soon as I created the key, I file .json was downloaded with the credentials.
I copied the content of that file and paste into ServiceAccountCredentials.fromJson function, so the credentials cannot be wrong.
And even if the credentials were wrong, why cannot I see an error that clientViaServiceAccount call is failing?
I am catching any error and print them in the screen with the last }).catchError((e) => print(e));.
For some reason the call clientViaServiceAccount is not doing anything and I cannot understand how to find the reason for that.

This method works in the cloud. If you want to use it in the flutter app, you have to get the user authenticated using the following plugin,
extension_google_sign_in_as_googleapis_auth
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With