Reputation: 3407
I want to add an event to a google calendar in Java. I don't want to use OAuth which is web based one. Using simple java code i want to add an event, the authentication for google calendar i use is static. My application generates events and posts to Google calendar. I use the following code to create an event and post to calendar.
Event event = new Event();
event.setSummary("Appointment");
event.setLocation("Somewhere");
ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail("attendeeEmail"));
// ...
event.setAttendees(attendees);
Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + 3600000);
DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
Event createdEvent = service.events().insert("primary", event).execute();
System.out.println(createdEvent.getId());
I have gone through the following links which explains about web OAuth. But i don't need such a complex thing.Google Service creation
How can i get the service object here ???
Please help me if anyone knows and example code is highly appreciated
Upvotes: 1
Views: 2660
Reputation: 3407
Following code worked for me, Service class is
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collections;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.common.io.Files;
/**
* @author Yaniv Inbar
*/
public class CalendarService {
/**
* Be sure to specify the name of your application. If the application name is {@code null} or
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
*/
private static final String APPLICATION_NAME = "ServiceCalendar";
/** E-mail address of the service account. */
private static final String SERVICE_ACCOUNT_EMAIL = "[email protected]";
/** Global instance of the HTTP transport. */
private static HttpTransport httpTransport;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
public Calendar configure() {
try {
try {
httpTransport = new NetHttpTransport();
// check for valid setup
if (SERVICE_ACCOUNT_EMAIL.startsWith("Enter ")) {
System.err.println(SERVICE_ACCOUNT_EMAIL);
System.exit(1);
}
URL loc = this.getClass().getResource("/ServiceApp-13c8dce63281.p12");
String path = loc.getPath();
File file = new File(path);
String p12Content = Files.readFirstLine(file, Charset.defaultCharset());
if (p12Content.startsWith("Please")) {
System.err.println(p12Content);
System.exit(1);
}
// service account credential (uncomment setServiceAccountUser for domain-wide delegation)
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR))
.setServiceAccountPrivateKeyFromP12File(file)
.build();
Calendar client = new com.google.api.services.calendar.Calendar.Builder(
httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
System.out.println("Client : "+client);
return client;
} catch (IOException e) {
System.err.println(e.getMessage());
}
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
return null;
}
}
Test Class is :
import java.util.ArrayList;
import java.util.Date;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventAttendee;
import com.google.api.services.calendar.model.EventDateTime;
public class CalenderEventTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Event event = new Event();
Calendar service =null;
event.setSummary("Calendar Testing");
event.setLocation("US");
event.setDescription("Desired description");
ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail("[email protected]"));
// ...
event.setAttendees(attendees);
// set the number of days
java.util.Calendar startCal = java.util.Calendar.getInstance();
startCal.set(java.util.Calendar.MONTH, 11);
startCal.set(java.util.Calendar.DATE, 26);
startCal.set(java.util.Calendar.HOUR_OF_DAY, 9);
startCal.set(java.util.Calendar.MINUTE, 0);
Date startDate = startCal.getTime();
java.util.Calendar endCal = java.util.Calendar.getInstance();
endCal.set(java.util.Calendar.MONTH, 11);
endCal.set(java.util.Calendar.DATE, 26);
endCal.set(java.util.Calendar.HOUR_OF_DAY, 18);
endCal.set(java.util.Calendar.MINUTE, 0);
Date endDate = endCal.getTime();
DateTime start = new DateTime(startDate);
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate);
event.setEnd(new EventDateTime().setDateTime(end));
service = new CalendarService().configure();
Event createdEvent = service.events().insert("primary", event).execute();
System.out.println("Data is :"+createdEvent.getId());
}
}
Maven dependency is :
<properties>
<google.version>1.19.0</google.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-calendar</artifactId>
<version>v3-rev96-1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.19.0</version>
</dependency>
</dependencies>
And one should put the ServiceApp-13c8dce63281.p12
file in resources
folder of maven.
It will work undoubtedly.
Hope it helps someone in need.
Upvotes: 1
Reputation: 184
First go to Google developer console, create a project, get a service account and private key file. Share the calendar to the service account email.
HttpTransport TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT)
.setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR))
.setServiceAccountPrivateKeyFromP12File(new File(key.p12))
.build();
service = new Calendar.Builder(TRANSPORT, JSON_FACTORY, credential)
.build();
Upvotes: 1