0

我已经研究了几个小时的文档,但我就是想不通。我只需要访问我自己的日历。那么我必须采取哪些步骤?身份验证如何工作?

谢谢,乔

4

1 回答 1

0

I used the this code to access the calendar list i.e., 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.

于 2013-01-05T11:51:37.530 回答