Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将CalendarEvent插入除“事件”之外的其他日历;“初级”;_Java_Google Api_Google Calendar Api - Fatal编程技术网

Java 将CalendarEvent插入除“事件”之外的其他日历;“初级”;

Java 将CalendarEvent插入除“事件”之外的其他日历;“初级”;,java,google-api,google-calendar-api,Java,Google Api,Google Calendar Api,如何将CalendarEvent插入到由其Id指定的特定日历中 在我的应用程序中,我正在扫描所有日历并将它们添加到ArrayList中,如下所示: for (CalendarListEntry calendarEntry : this.calendars.getItems()) { this.calendarIDs.add(calendarEntry.getId()); } 我找到的唯一代码片段是: Event createdEvent = service.events().ins

如何将CalendarEvent插入到由其Id指定的特定日历中

在我的应用程序中,我正在扫描所有日历并将它们添加到ArrayList中,如下所示:

for (CalendarListEntry calendarEntry : this.calendars.getItems()) {
      this.calendarIDs.add(calendarEntry.getId());
}
我找到的唯一代码片段是:

Event createdEvent = service.events().insert("primary", event).execute();
我试着把feed的url改为private,比如


请尝试以下代码片段:

第1段:

public void authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    List <String> scopes = new LinkedList<String>();
    scopes.add(scope);
    AuthorizationCodeRequestUrl authorize = new GoogleAuthorizationCodeRequestUrl(client_id, redirect_uri, scopes);
    authorize.setRedirectUri(redirect_uri);
    String authorize_url              = authorize.build();
    log.info(authorize_url);
    response.sendRedirect(authorize_url);
}
公共无效身份验证(HttpServletRequest请求、HttpServletResponse响应)引发IOException、ServletException{
列表范围=新建LinkedList();
范围。添加(范围);
AuthorizationCodeRequestUrl Authorization=新的GoogleAuthorizationCodeRequestUrl(客户端id、重定向uri、作用域);
authorize.setRedirectUri(重定向uri);
字符串authorize_url=authorize.build();
日志信息(授权url);
sendRedirect(授权url);
}
SnippetOne负责OAuth并将程序重定向到重定向uri。变量scope、client_id、cliend_secret和scope的值来自googleapi控制台

片段2:

public void importCalendarList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String code = request.getParameter("code");
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleTokenResponse res = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, client_id, client_secret, code, redirect_uri).execute();
    ]String accessToken = res.getAccessToken();
    Calendar.Builder builder = new Calendar.Builder(transport, jsonFactory, null);  
    builder.setCalendarRequestInitializer(new CalendarRequestInitializer(accessToken));
    Calendar calendarService = builder.build();
    Calendar.CalendarList.List list = calendarService.calendarList().list();
    list.setOauthToken(accessToken);
    List <CalendarListEntry>list1=list.execute().getItems();
    String id = list1.get(0).getId();
    p.write(id);

    for(CalendarListEntry temp:list1) {
        p.println(temp.getSummary());
        temp.getId();
    }

    Event e = new Event();

    e.setSummary("Test event");
    e.setLocation("Adaptavant");

    Date startDate = new Date();
    Date endDate = new Date(startDate.getTime() + 3600000);
    DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
    e.setStart(new EventDateTime().setDateTime(start));
    DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
    e.setEnd(new EventDateTime().setDateTime(end));
    Event insertedEvent = calendarService.events().insert(id, e).setOauthToken(accessToken).execute();

    p.println(insertedEvent.getId());
public void importCalendarList(HttpServletRequest请求,HttpServletResponse响应)抛出IOException,ServletException{
字符串代码=request.getParameter(“代码”);
HttpTransport传输=新的NetHttpTransport();
JsonFactory JsonFactory=新的JacksonFactory();
GoogleTokenResponse res=新的GoogleAuthorizationCodeTokenRequest(传输、jsonFactory、客户端id、客户端机密、代码、重定向uri)。执行();
]字符串accessToken=res.getAccessToken();
Calendar.Builder=new Calendar.Builder(transport,jsonFactory,null);
setCalendarRequestInitializer(新的CalendarRequestInitializer(accessToken));
Calendar calendarService=builder.build();
Calendar.CalendarList.List=calendarService.CalendarList().List();
list.setOauthToken(accessToken);
List list1=List.execute().getItems();
String id=list1.get(0.getId();
p、 写入(id);
对于(CalendarListEntry临时列表:list1){
p、 println(temp.getSummary());
temp.getId();
}
事件e=新事件();
e、 集合摘要(“测试事件”);
e、 设置位置(“Adaptivant”);
日期开始日期=新日期();
Date endDate=新日期(startDate.getTime()+3600000);
DateTime start=新的日期时间(startDate,TimeZone.getTimeZone(“UTC”);
e、 setStart(neweventdatetime().setDateTime(start));
DateTime end=newdatetime(endDate,TimeZone.getTimeZone(“UTC”);
e、 setEnd(neweventdatetime().setDateTime(end));
Event insertedEvent=calendarService.events().insert(id,e).setOauthToken(accessToken).execute();
p、 println(insertedEvent.getId());
}

代码段2获取日历列表并获取其中一个日历的id,然后在该日历中创建一个事件。如果要将事件添加到主日历,则主日历的id是用于登录的gmail id

更多文件可在此处找到:


当用户是google用户但未连接到google plus时,通常会发生这种情况


在这个调用中它也应该爆炸:service.people().get(“me”).execute()

您可以在“API资源管理器”中查看示例用法。日历方法的链接:。测试它-也许你找到了之后的方法。看起来你可能在混合版本-提要URL来自v2,但JSON响应是v3。您能否澄清URL在代码中的位置?
public void authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    List <String> scopes = new LinkedList<String>();
    scopes.add(scope);
    AuthorizationCodeRequestUrl authorize = new GoogleAuthorizationCodeRequestUrl(client_id, redirect_uri, scopes);
    authorize.setRedirectUri(redirect_uri);
    String authorize_url              = authorize.build();
    log.info(authorize_url);
    response.sendRedirect(authorize_url);
}
public void importCalendarList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String code = request.getParameter("code");
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleTokenResponse res = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, client_id, client_secret, code, redirect_uri).execute();
    ]String accessToken = res.getAccessToken();
    Calendar.Builder builder = new Calendar.Builder(transport, jsonFactory, null);  
    builder.setCalendarRequestInitializer(new CalendarRequestInitializer(accessToken));
    Calendar calendarService = builder.build();
    Calendar.CalendarList.List list = calendarService.calendarList().list();
    list.setOauthToken(accessToken);
    List <CalendarListEntry>list1=list.execute().getItems();
    String id = list1.get(0).getId();
    p.write(id);

    for(CalendarListEntry temp:list1) {
        p.println(temp.getSummary());
        temp.getId();
    }

    Event e = new Event();

    e.setSummary("Test event");
    e.setLocation("Adaptavant");

    Date startDate = new Date();
    Date endDate = new Date(startDate.getTime() + 3600000);
    DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
    e.setStart(new EventDateTime().setDateTime(start));
    DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
    e.setEnd(new EventDateTime().setDateTime(end));
    Event insertedEvent = calendarService.events().insert(id, e).setOauthToken(accessToken).execute();

    p.println(insertedEvent.getId());