google calendar api java

Solutions on MaxInterview for google calendar api java by the best coders in the world

showing results for - "google calendar api java"
Serena
25 Jan 2018
1If you just need to access a particular set of calendars, I would create a service account and share the necessary calendars with that account.
2
3To do so:
4
5Create a "Service Account" in this Cloud Console (it is found under "Web application" / "Certificate").
6Download the private key and store in a safe place.
7Take note of the email address associated with the service account.
8Share (via Calendar user interface) any necessary calendars with this email address.
9Install Google API Java Client libraries (https://developers.google.com/api-client-library/java/apis/).
10Then you should be able to use the following code:
11
12import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
13import com.google.api.client.json.gson.GsonFactory;
14import java.io.File;
15import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
16import java.util.Arrays;
17import com.google.api.services.calendar.Calendar;
18
19GoogleCredential credentials = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
20  .setJsonFactory(new GsonFactory())
21  .setServiceAccountId("<service account email address>@developer.gserviceaccount.com")
22  .setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar.readonly"))
23  .setServiceAccountPrivateKeyFromP12File(new File("<private key for service account in P12 format>-privatekey.p12"))
24.build();
25Calendar client = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(), new GsonFactory(), credentials).build();
26client.<do calendar stuff>.execute();
27If instead you are a domain administrator who needs to access calendars for all Google Apps accounts that are part of your domain without consent from individual users, then instead of step 4 above:
28
29Take note of the client ID associated with the service account. This can be found in the client_secrets.json file - typically in the form 1234.apps.googleusercontent.com.
30Authorize this client to make requests on behalf of users in your organization. See https://support.google.com/a/answer/162106?hl=en for steps - use whichever scopes you will be requesting later.
31You should now be able to write code like the following:
32
33import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
34import com.google.api.client.json.gson.GsonFactory;
35import java.io.File;
36import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
37import java.util.Arrays;
38import com.google.api.services.calendar.Calendar;
39
40GoogleCredential credentials = new GoogleCredential.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
41  .setJsonFactory(new GsonFactory())
42  .setServiceAccountId("<service account email address>@developer.gserviceaccount.com")
43  .setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar"))
44  .setServiceAccountPrivateKeyFromP12File(new File("<private key for service account in P12 format>-privatekey.p12"))
45  .setServiceAccountUser("<domain user whose data you need>@yourdomain.com")
46.build();
47Calendar client = new Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(), new GsonFactory(), credentials).build();
48client.<do calendar stuff as that user>()