2012-06-28 3 views
0

몇 시간 동안 문서 작업을 해봤지만 그 사실을 알 수는 없습니다. 내 자신의 캘린더에 대한 액세스 만 필요합니다. 그렇다면 어떤 단계를 밟아야합니까? 인증은 어떻게 작동합니까?Google 캘린더 API : JSON으로 이벤트 받기

덕분에, 조

+0

정보 : Java로 작업하고 있습니다. –

+1

데스크톱 응용 프로그램, 웹 응용 프로그램 또는 모바일 응용 프로그램입니까? 각각의 경우 OAuth 2.0은 다르게 처리됩니다. 캘린더 API 사용에 관해서는 클라이언트 라이브러리의 설명서가 거의 존재하지 않기 때문에 Google의 Java 용 클라이언트 라이브러리를 사용하는 대신 자신의 솔루션을 롤백하는 것이 좋습니다. – HK1

+0

웹 응용 프로그램입니다. REST를 사용할 때 인증은 어떻게 작동합니까? 나는 내 자신의 달력 (비공개 달력)에만 액세스하려고합니다. –

답변

0

나는 이것이 내가 만든 캘린더의 세부 사항을 반환 캘린더 목록 즉 액세스하기 위해이 코드를 사용했다. 나는 그것이 도움이되기를 바랍니다. 나는 OAuth를 2.0 초안 (12)와 Google 캘린더 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); 
} 

기능은 위의 OAuth 인증을 담당하고 허용하거나 거부 화면에 사용자가 지시를 사용하고 있습니다. 허용 된 경우이 함수로 리디렉션됩니다.

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

이 함수 importCalendarList는 사용자가 가지고있는 모든 일정의 이름을 나열합니다. CalendarListEntry 형의 List는, 달력의 Object 표현입니다. 따라서 게터에서 캘린더 중 하나의 고유 ID 또는 캘린더의 시간대를 가져올 수 있습니다.

캘린더 API를 알기 위해이 프로그램을 작성했습니다. 필자가 글쓰기를 시작하기 전에 Google api 콘솔로 가서 이러한 것들을 설정했습니다.

클라이언트 ID, 리디렉션 URI, 범위 및 클라이언트 비밀. 이것을 설정하고 프로그램에서 사용하십시오. 캘린더 목록을 보여 주기만하면 캘린더에서 일정을 가져오고, 추가하고, 업데이트하고, 삭제할 수 있으며, 캘린더 자체에서 모든 작업을 수행 할 수 있습니다. Google 캘린더 API에 대한 포괄적 인 문서는 여기에서 확인할 수 있습니다.

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

마지막주의 사항 웹 응용 프로그램에서이 작업을 수행했습니다. 희망이 도움이됩니다.