I am trying to create a java class for Event Creation using google service account with downloaded json credentials. I have given the Owner permission to the generated gmail id for authentication and have added Calendar Scopes as well.
Error Unexpected error refreshing access token
[com.google.auth.oauth2.OAuth2Credentials.unwrapDirectFuture(OAuth2Credentials.java:323),
com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:170),
com.google.auth.oauth2.ServiceAccountCredentials.getRequestMetadata(ServiceAccountCredentials.java:967),
com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:96),
com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:91),
com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:415),
com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:525),
com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:466),
com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:576)]
Here is code for authentication where the json from json file is being passed.
Also, do we need to add code for refresh token when we are passing credentials using service account json creds?
authenticate(){
String json; //json credentials.
json = String.format(jsonFormat, null);
return GoogleCredentials.fromStream(new ByteArrayInputStream(json.getBytes()))
.createScoped(scopeList)
.createDelegated((GMAIL_USER));
}
createEvent(){
GoogleCredentials credentials = null;
try {
credentials = authenticate();
System.out.println("Got Credentials: " + credentials);
System.out.println("---------------Got Credentials-----------------------");
} catch (Exception e) {
System.out.println("Error :"+ e.getMessage());
}
try {
// Connecting to the service
JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)
.setApplicationName(APPLICATION_NAME).build();
Event event = new Event().setSummary((E_SUMMARY)).setLocation((E_LOCATION)).setDescription((E_DESC));
String temp = (E_START_DATE);// local variable
DateTime startDateTime = new DateTime(temp);
EventDateTime start = new EventDateTime().setDateTime(startDateTime).setTimeZone("America/Los_Angeles"); // Change
// America/Los_Angeles
// check
event.setStart(start);
temp = (E_END_DATE);// re-assigned to end time.
DateTime endDateTime = new DateTime(temp);
EventDateTime end = new EventDateTime().setDateTime(endDateTime).setTimeZone("America/Los_Angeles");
event.setEnd(end);
String[] recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" };
event.setRecurrence(Arrays.asList(recurrence));
EventAttendee[] attendees = new EventAttendee[] { new EventAttendee().setEmail("abc@gmail.com"),
new EventAttendee().setEmail("def@gmail.com"), };
event.setAttendees(Arrays.asList(attendees));
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("email").setMinutes(24 * 60),
new EventReminder().setMethod("popup").setMinutes(10), };
Event.Reminders reminders = new Event.Reminders().setUseDefault(false)
.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
String calendarId = E_CALENDAR_TYPE;
event = service.events().insert(calendarId, event).execute();
System.out.printf("Event created: %s\n", event.getHtmlLink());
} catch (Exception e) {
System.out.println("--------Inside last catch block --------");
System.out.println("Error " + e.getMessage());
System.out.println(Arrays.toString(e.getStackTrace()));
}
}