0
votes

I'm trying to retrieve all the events programmed within a domain entreprise.tn using Google Calendar API.

On google admin console, I create a new project and a new service account with owner role as described by that thread.

I enabled Google Calendar API and Admin SDK like described by that thread.

the list of scopes added on Admin console>Security are : https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group, https://www.googleapis.com/auth/admin.directory.resource.calendar, https://www.googleapis.com/auth/calendar.events.readonly, https://www.googleapis.com/auth/calendar.readonly

My code is:

        Calendar service = getCalendarService();
        List<Event> items = new ArrayList<Event>();
        String pageToken = null;
        do
        {
            Events events = service.events().list("service-account-esp1@my-first-project-274515.iam.gserviceaccount.com").setPageToken(pageToken).execute();
            items = events.getItems();

            for (Event event : items)
            {
                System.out.println(event.getSummary());
            }
          pageToken = events.getNextPageToken();
        } while (pageToken != null);

        if (items.isEmpty())
        {
            System.out.println("Empty");
        }
        else
        {
             System.out.println("Exists");
        }

the file my-first-project-274515-361633451f1c.json is the generated file when creating the service account and performing G Suite Domain-Wide Delegation of Authority.

the service-account-esp1@my-first-project-274515.iam.gserviceaccount.com is the client email

It looks ok, all the required configurations are done. How evere, I got that exception:

avr. 18, 2020 12:28:59 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly AVERTISSEMENT: Unable to set permissions for C:\Users\Administrateur\credentials, because you are running on a non-POSIX file system. Charge Calendars: Sat Apr 18 12:28:59 BST 2020 a Exception in thread "main" java.lang.IllegalArgumentException at com.google.common.base.Preconditions.checkArgument(Preconditions.java:128) at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:35) at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:82) at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder.(GoogleAuthorizationCodeFlow.java:197) at tn.esprit.spring.google.calendar.Service.getCredentials(Service.java:75) at tn.esprit.spring.google.calendar.Service.getCalendarService(Service.java:90) at tn.esprit.spring.google.calendar.Service.main(Test.java:102)

it's blocked on GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();

Could you please tell me what I missed ?. Thanks in advance.

1
Have you tried using the Calendar Java Quickstart and the modify what you need?Kessy

1 Answers

2
votes

You should use mail user instead:

Events events = service.events().list([email protected])
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();

HTH