【发布时间】:2020-11-13 10:02:28
【问题描述】:
@Component
public class GoogleCalendarSyncListener implements ApplicationListener<CourseLessonGoogleCalendarNeedToBeUpdatedEvent> {
private String getEventId(Lesson parameter) { // encapsulator method
return "cita" + parameter.getId();
}
// TODO bu daha genel olabilir.
@Configuration
static class Configurator {
@Bean
public Calendar client() {
List<String> scopes = Lists.newArrayList(CalendarScopes.CALENDAR);
try {
Calendar client = CalendarUtils.createCalendarClient(scopes);
return client;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private LessonService lessonService;
@Autowired
private LessonUserService lessonUserService;
@Autowired
private Calendar client; // bunu da confige taşımak lazım
@Override
public void onApplicationEvent(CourseLessonGoogleCalendarNeedToBeUpdatedEvent courseLessonGoogleCalendarNeedToBeUpdatedEvent) {
try {
ProductType productType = courseLessonGoogleCalendarNeedToBeUpdatedEvent.getCourseRegistration().getCourse().getCategory().getProductType();
Course course = courseLessonGoogleCalendarNeedToBeUpdatedEvent.getCourseRegistration().getCourse();
List<Lesson> lessons = lessonService.loadLessonsByCourse(course);
int lessonCount = lessons.size();
for (Lesson lesson : lessons) {
String description = "";
try {
String eventId = getEventId(lesson); // RFC2938 bu kurala uygun olmalı
DateTime start = new DateTime(lesson.getDate(), TimeZone.getTimeZone("UTC+03:00"));
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(lesson.getDate());
cal.add(java.util.Calendar.MINUTE, lesson.getLessonPricingTemplate().getDurationMinutes());
cal.getTime();
DateTime end = new DateTime(cal.getTime(), TimeZone.getTimeZone("UTC+03:00"));
description += lesson.getCourse().getCatName();
String color = "";
for (LessonUser lessonuser : lessonUserService.loadByLesson(lesson)) {
description += " " + lessonuser.getUser().getPrettyName();
switch (lessonuser.getUser().getUsername()) {
case "bgor":
color = Constants.BGORCOLOR;
break;
}
}
// TODO error checdking
description += lessonService.lessonIndex(lesson) + "/" + lessonCount;
Event event = new Event();
event.setColorId(color);
event.setId(eventId);
event.setReminders(new Event.Reminders().setUseDefault(false));
event.setSummary(description);
event.setStart(new EventDateTime().setDateTime(start));
event.setEnd(new EventDateTime().setDateTime(end));
event.setDescription(lesson.getCourse().getCatName());
List<EventAttendee> attendees = new ArrayList<>();
EventAttendee admin = new EventAttendee();
admin.setEmail("mail@btmuzik.com");
attendees.add(admin);
List<LessonUser> lessonUsers = lessonUserService.loadByLesson(lesson);
lessonUsers.forEach(lu -> {
EventAttendee attendee = new EventAttendee();
if (lu.getUser().getEmail() != null) {
attendee.setEmail(lu.getUser().getEmail());
attendees.add(attendee);
}
});
event.setAttendees(attendees);
Event.Creator creator = new Event.Creator();
Event.Organizer organizer = new Event.Organizer();
creator.setEmail("mail@btmuzik.com");
organizer.setEmail("mail@btmuzik.com");
event.setCreator(creator);
event.setOrganizer(organizer);
logger.info("event %s (%s)\n", event.getSummary(), start);
saveOrUpdateEvent(event);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
// By existing conf tomcat/.store/bt-calendar-sync/StoredCredentials must exist
// These are copy of btmuzikevi-parent/tokens/StoredCredential created by CalendarReader
private Event saveOrUpdateEvent(Event event) {
try {
System.out.println("client = " + client.getBaseUrl());
System.out.println("client = " + client.getRootUrl());
System.out.println("event.getId() = " + event.getId());
List<String> scopes = Lists.newArrayList(CalendarScopes.CALENDAR);
client = CalendarUtils.createCalendarClient(scopes);
Calendar.Events.List request = client.events().list("primary");
System.out.println("--------------------------------- ");
event = client.events().update("primary", event.getId(), event).execute(); // updated event
System.out.println(event.getId()+ " HUHA = " + event.getStart());
return event;
} catch (GoogleJsonResponseException e) { // burası özel bir değer veriyor
try {
event = client.events().insert("primary", event).execute();
return event;
} catch (IOException ioException) { // bu çok özel değil ama tüm detayı içeriyor
throw new RuntimeException(ioException); // tamamını runtimeexception olarak dönüyoruz.
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
请求的 Oauth2 范围:
- Google 日历 API .../auth/calendar 查看、编辑、共享和永久删除您可以使用 Google 日历访问的所有日历
- Google Calendar API .../auth/calendar.acls 查看和更改您拥有的 Google 日历的共享权限
- Google Calendar API .../auth/calendar.events 查看和编辑您所有日历上的活动
CalendarQuickstart 或 CalendarReader 很好,它们从日历中读取,但是: 当我尝试写入日历时,我得到:
授权错误 错误 400:invalid_request 缺少必需参数:response_type
我错过了什么? 提前感谢您的任何帮助
【问题讨论】:
-
您需要重新编写代码以找出导致错误的参数。从基本的必需参数(开始和结束)开始,然后逐渐添加更多。一旦你知道哪个参数是罪魁祸首,排除故障就会更容易。
标签: java google-oauth google-calendar-api google-api-java-client