遇见谷歌日历API

7
我该如何在Java的Google日历API中添加Google Meet?请帮帮我。我还没有理解谷歌的文档。https://developers.google.com/calendar/create-events 在这里提供源代码。在此,我想要使用用户的Gmail账户创建事件,我没有任何G-suite账户。
Event event = new Event()
    .setSummary(title)
    .setLocation(location)
    .setDescription(description);

DateTime startDateTime = new DateTime( date +"T"+startTime+"+06:00" );//"2020-05-05T11:00:00+06:00");
EventDateTime start = new EventDateTime()
    .setDateTime(startDateTime)
    .setTimeZone("Asia/Dhaka");
event.setStart(start);

DateTime endDateTime = new DateTime(date +"T"+endTime+"+06:00");//"2020-05-05T12:00:00+06:00");
EventDateTime end = new EventDateTime()
    .setDateTime(endDateTime)
    .setTimeZone("Asia/Dhaka");
event.setEnd(end);

String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=1"};
event.setRecurrence(Arrays.asList(recurrence));

EventAttendee attendees[];

attendees = new EventAttendee[allAttendees.size()];

for(int i=0; i<allAttendees.size(); i++){
    // System.out.println(allAttendees.get(i));
    attendees[i] = new EventAttendee().setEmail(allAttendees.get(i));
}
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 = "primary";

try {
    abc = service.events().insert(calendarId, event);
} catch (IOException e) {
    e.printStackTrace();
}

try {
    event = service.events().insert(calendarId, event).execute();
} catch (IOException e) {
    e.printStackTrace();
}

String meetingId = event.getHangoutLink();
System.out.println("What is meeting ID? = "+meetingId);

嗨!如果您能提供一些您已经编写的代码,那将非常有帮助。 - Aljaž Medič
提供源代码 - TuitionAppSpl
3个回答

7

答案

您需要使用Google日历的JAVA API文档

您需要创建一个新的Meet请求,然后将其附加到当前事件之前,并在此之前通过将会议数据版本设置为1来启用conferenceDataVersion。在使用以下代码之前,请确保您已经完成了设置

代码

Event event = new Event()
                        .setSummary(title)
                        .setLocation(location)
                        .setDescription(description);

// Your previous code

/* The code needed - START */

ConferenceSolutionKey conferenceSKey = new ConferenceSolutionKey();
conferenceSKey.setType("eventHangout"); // Non-G suite user
CreateConferenceRequest createConferenceReq = new CreateConferenceRequest();
createConferenceReq.setRequestId("3whatisup3"); // ID generated by you
createConferenceReq.setConferenceSolutionKey(conferenceSKey);
ConferenceData conferenceData = new ConferenceData();
conferenceData.setCreateRequest(createConferenceReq);
event.setConferenceData(conferenceData); // attach the meeting to your event

/* The code needed - END */

String calendarId = "primary";

// There’s no need to declare the try-catch block twice

try {
    /* Code changes - START */

    // .setConferenceDataVersion(1) enables the creation of new meetings
    event = service.events().insert(calendarId, event).setConferenceDataVersion(1).execute();

    /* Code changes - END */

} catch (IOException e) {
    e.printStackTrace();
}

String meetingId = event.getHangoutLink();
System.out.println("What is meeting ID? = "+meetingId);

参考资料

Google Calendar JAVA API: Event.setConferenceData

Google Calendar JAVA API: ConferenceData.setCreateRequest

Google Calendar JAVA API: CreateConferenceRequest.setRequestId

Google Calendar JAVA API: ConferenceSolutionKey.setType

Google Calendar JAVA API: Calendar.Events.Insert.setConferenceDataVersion 最重要的部分


我已经使用了以下依赖项。implementation('com.google.api-client:google-api-client-android:1.23.0') { exclude group: 'org.apache.httpcomponents' } implementation('com.google.apis:google-api-services-calendar:v3-rev305-1.23.0') { exclude group: 'org.apache.httpcomponents' } - TuitionAppSpl
请您详细描述一下您遇到的错误是什么? - Jose Vasquez
没错,兄弟。你的代码运行正常。非常感谢。愿上帝保佑你。<3 - TuitionAppSpl
2
很高兴能够帮助!为了文档记录,如果可以的话,请接受对您有帮助的答案 - 这有助于未来遇到相同问题的其他人找到解决方案。 - Jose Vasquez

3

对我而言,可行的最终代码如下:

 Event event = new Event()
            .setSummary(title)
            .setLocation(location)
            .setDescription(description);


    DateTime startDateTime = new DateTime( date +"T"+startTime+"+06:00" );//"2020-05-05T11:00:00+06:00");
    EventDateTime start = new EventDateTime()
            .setDateTime(startDateTime)
            .setTimeZone("Asia/Dhaka");
    event.setStart(start);

    DateTime endDateTime = new DateTime(date +"T"+endTime+"+06:00");//"2020-05-05T12:00:00+06:00");
    EventDateTime end = new EventDateTime()
            .setDateTime(endDateTime)
            .setTimeZone("Asia/Dhaka");
    event.setEnd(end);

    String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=1"};
    event.setRecurrence(Arrays.asList(recurrence));

  /*  s1 = "abc@gmail.com";
    s2 = "xyz@gmail.com";

    EventAttendee[] attendees = new EventAttendee[] {
            new EventAttendee().setEmail(s1),
            new EventAttendee().setEmail(s2),
    };*/



    EventAttendee attendees[];

    attendees = new EventAttendee[allAttendees.size()];

    for(int i=0; i<allAttendees.size(); i++){
       // System.out.println(allAttendees.get(i));
        attendees[i] = new EventAttendee().setEmail(allAttendees.get(i));
    }
    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);


    ConferenceSolutionKey conferenceSKey = new ConferenceSolutionKey();
    conferenceSKey.setType("hangoutsMeet"); // Non-G suite user
    CreateConferenceRequest createConferenceReq = new CreateConferenceRequest();
    createConferenceReq.setRequestId("3whatisup3"); // ID generated by you
    createConferenceReq.setConferenceSolutionKey(conferenceSKey);
    ConferenceData conferenceData = new ConferenceData();
    conferenceData.setCreateRequest(createConferenceReq);
    event.setConferenceData(conferenceData);

    String calendarId = "primary";

    try {
        event = service.events().insert(calendarId, event).setConferenceDataVersion(1).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.printf("Event created: %s\n", event.getHtmlLink());
    System.out.printf("Hangout Link %s\n", event.getHangoutLink());

如果您能解释一下为什么这对您来说是一个解决方案,您需要做什么,那将会很有帮助。这样像我这样的人在以后查看您的代码时就会有上下文。 - viam0Zah
以上代码对我来说是一个解决方案,因为我想在创建日历事件时创建Google Meet链接。使用这段代码,Google Meet链接已成功地与日历事件创建。 - TuitionAppSpl
从哪里生成ID? - rudeTool

1

@Jose Vasquez的答案是正确的,除了一件事情。 我改变了这一行

conferenceSKey.setType("eventHangout");

转换成这个

conferenceSKey.setType("hangoutsMeet"); 

然后一切都正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接