简体   繁体   中英

Google Calendar API: Get events as JSON

I've been working through the documentation for hours, but I just can't figure it out. I need only access to my own calendar. So what steps do I have to take? How does the authentication work?

Thanks, Joe

I used the this code to access the calendar list ie, this returns the details of the calendars that I created. I hope it helps. I am using OAuth 2.0 draft 12 and Google Calendar API v3

    @RequestMapping(value="/authenticate.do" , method={ RequestMethod.GET , RequestMethod.POST })
    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);
}

The function above takes care of the OAuth authentication and directs the user to the screen where they allow or deny. If they allow they are redirected to this function.

@RequestMapping(value="/importCalendar.do", method={ RequestMethod.GET , RequestMethod.POST })
public void importCalendarList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    PrintWriter p = response.getWriter();

    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();
    } 
}

This function importCalendarList lists the names of all the calendars that the user has. The List of type CalendarListEntry is the Object representation of your calendars. So from getters you can get, for instance, the uniqueid of one of the calendars or the time zone of a calendar.

I wrote this program for knowing calendar api. Before I started writing I went to google api console to set these things up.

client id, redirect uri, scope and client secret. Set these up and use them in the program. This just shows the calendar list however you can also get, add, update and delete events from/to a calendar or do all these operations on a calendar itself. The comprehensive documentation for Google Calendar API is found here:

https://google-api-client-libraries.appspot.com/documentation/calendar/v3/java/latest/index.html

and one last note. I did this from a web application. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM