我想为我的一个应用程序实现日历/事件。所以我决定使用 Google Calendar API (Java)。但是我看不到它的实现的 Web 部分(前端)。任何人都可以帮助我获得 Google Calendar API(包括前端)的整个实现。提前致谢。
2 回答
也许这就是您正在寻找的:
https://developers.google.com/accounts/docs/OAuth2WebServer和https://developers.google.com/google-apps/calendar/auth
这使您能够编写一个基于 Web 的应用程序,使用户能够登录到 google 并让您查看或修改用户日历
授权后(此处的漂亮序列图:https ://developers.google.com/accounts/docs/OAuth2#webserver ),您可以使用 Google Java API 添加删除或查看日历或日历项或直接从 Javascript https 使用 REST: //developers.google.com/google-apps/calendar/firstapp#rest
或者,您可以将旧版日历小部件用于此处的公共日历: https ://developers.google.com/gdata/samples/cal_sample
**,首先,您必须在console.developer.google.com 上创建一个项目并启用日历API,然后您必须设置OauthScope 并创建Credentials,然后下载credentials.json 文件并粘贴到您的项目中,然后运行它程序将生成您的事件**
public class Test {
private static final String APPLICATION_NAME = "GoogleCalenderApi";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
InputStream in = Test.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void main(String... args) throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)).setApplicationName(APPLICATION_NAME).build();
Event event = new Event();
event.setSummary("Title of the Event");
event.setLocation("Location");
event.setDescription("Something about the event");
event.setId("event id");
ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail("example@gmail.com"));
attendees.add(new EventAttendee().setEmail("1234@gmail.com"));
event.setAttendees(attendees);
String startDate = "2019-10-16 09:00:00";
Timestamp sDate = Timestamp.valueOf(startDate);
String endDate = "2019-10-18 18:15:00";
Timestamp eDate = Timestamp.valueOf(endDate);
DateTime start = new DateTime(sDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(eDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
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);
Event createdEvent = service.events().insert("primary", event).execute();
System.out.println(createdEvent);
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
if (items.isEmpty()) {
System.out.println("No upcoming events found.");
} else {
System.out.println("Upcoming events");
for (Event event1 : items) {
DateTime start1 = event1.getStart().getDateTime();
if (start1 == null) {
start1 = event1.getStart().getDate();
}
System.out.println(event1);
//System.out.printf("%s (%s)\n", event1.getId(), start1);
}
}
}