我想在Java中将事件添加到Google日历中。 我不想使用基于网络的OAuth。使用简单的java代码我想添加一个事件,我使用的谷歌日历的身份验证是静态的。我的应用程序会为Google日历生成活动和帖子。 我使用以下代码创建一个事件并发布到日历。
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());
我已经通过以下链接解释了有关网络OAuth的信息。但我不需要这么复杂的事情。Google Service creation
我如何在这里获得服务对象?
如果有人知道并且示例代码得到高度赞赏,请帮助我
答案 0 :(得分:1)
首先转到Google开发人员控制台,创建项目,获取服务帐户和私钥文件。 将日历分享到服务帐户电子邮件。
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();
答案 1 :(得分:1)
以下代码适用于我,服务类
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 = "135965950222_test@developer.gserviceaccount.com";
/** 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;
}
}
测试类是:
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("tester@gmail.com"));
// ...
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依赖是:
<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>
并且应该将ServiceApp-13c8dce63281.p12
文件放在maven的resources
文件夹中
毫无疑问,它会起作用
希望它可以帮助有需要的人。