구글은 그래서 당신의 OAuth 2.0 및 Google 캘린더 API v3으로 마이그레이션 시간이 20 2012 년 4 월부터 AuthSub 서비스를 추천하지 않습니다. 먼저이 다음 링크에서 jar 파일을 다운로드 :
https://google-api-client-libraries.appspot.com/download/library/calendar/v3/java
http://google-oauth-java-client.googlecode.com/files/google-oauth-java-client-1.13.1-beta.zip
프로젝트에서 기존 일정 및 AuthSub를 jar 파일을 제거하고이 링크에서 jar 파일을 추가합니다.
다음 Google api 콘솔로 이동하여 클라이언트 ID, 클라이언트 비밀 번호를 얻고 리디렉션 URI를 만듭니다. 동일한 api 콘솔에서 Google 캘린더 API를 사용하도록 설정합니다.
사용자를 인증하고 출력에 가져온 새로 고침 토큰을 저장하고 캘린더를 오프라인으로 액세스 할 수 있도록 저장해야하는 일정을 보여주는 샘플 코드를 제공하고 있습니다.
다음 기능은 OAuth 인증 용입니다.
public void authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String client_id = "xxxx";
String redirect_uri = "xxxxxx";
String scope = "https://www.googleapis.com/auth/calendar";
String client_secret = "xxxxxx";
List <String> scopes;
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
scopes = new LinkedList<String>();
scopes.add(scope);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build();
GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl();
url.setRedirectUri(redirect_uri);
url.setApprovalPrompt("force");
url.setAccessType("offline");
String authorize_url = url.build();
response.sendRedirect(authorize_url);
}
당신은 변수 client_id
, client_secret
및 redirect_uri
에 값을 추가해야합니다. 이 모든 값은 Google API 콘솔에 있습니다.
권한 부여 기능은 나에게 액세스 토큰과 새로 고침 토큰을 제공하는 권한 부여 URL로 전달합니다. 그러나 액세스 토큰은 시간 간격 후에 만료됩니다. 따라서 액세스 토큰을 원할 경우 새로 고침 토큰을 저장하고이를 사용하여 달력 API에 액세스 할 때마다이를 생성해야합니다.
이 다음 함수는 액세스 토큰을 생성하고 토큰을 새로 고치고 사용자의 Google 캘린더에있는 캘린더 목록을 인쇄합니다.
public void importCalendarList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
HttpSession session = request.getSession();
String staffKey = (String) session.getAttribute("staffKey");
ContactJdo staffDetails = staff.getStaffDetail(staffKey);
String code = request.getParameter("code");
String calendarId="";
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build();
GoogleTokenResponse res = flow.newTokenRequest(code).setRedirectUri(redirect_uri).execute();
String refreshToken = res.getRefreshToken();
String accessToken = res.getAccessToken();
List <CalendarListEntry>list1= getCalendars(accessToken);
for(CalendarListEntry temp:list1) {
System.out.println(temp.getId());
}}
위 기능을 보면 액세스 및 새로 고침 토큰이 생성됩니다. 토큰 곳
public static String getAccessToken(String refreshToken, String client_id, String client_secret) throws IOException {
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleRefreshTokenRequest req = new GoogleRefreshTokenRequest(transport, jsonFactory, refreshToken, client_id, client_secret);
GoogleTokenResponse res = req.execute();
String accessToken = res.getAccessToken();
return accessToken;
}
스토어 새로 고침 당신은 달력 문서에 언급 된 모든 작업을 수행 할 수 있습니다 당신은 토큰 액세스를 생성하려면이 기능을 다시 사용합니다. 여기
https://google-api-client-libraries.appspot.com/documentation/calendar/v3/java/latest/index.html
그러나 사용자가 이미 허가했다 경우를 찾기? accessToken (+ appToken + appSecret) 만 있으면 어떻게됩니까? v3에서 가능합니까? getCalendars 메소드는 어떻게 구현됩니까? – Karussell
아, 작동해야합니다 : GoogleCredential credential = new GoogleCredential(). setAccessToken ("atoken"); 클라이언트 = 새 Calendar.Builder (새 NetHttpTransport(), 새 JacksonFactory(), 자격 증명) .setApplicationName (APPLICATION_NAME) .build(); -> https://groups.google.com/forum/#!topic/google-api-java-client/c33zpCkjJGk – Karussell
예,이 작업 + Google 캘린더 API 및 다음 maven deps에서 캘린더 API를 사용하도록 설정해야했습니다. 는 구글-API-서비스 달력 V3-rev47-1.15.0-RC com.google.api 클라이언트 구글-API를 com.google.apis -client 1 .15.0-RC com.google.http 클라이언트 구글-HTTP-클라이언트 jackson2 ' –
Karussell