2012-11-13 3 views
2

다음 링크에는 OAuth 2.0을 사용하여 Google 캘린더 API에 액세스해야하는 코드가 있습니다. 불행하게도이 라이브러리는 사용되지 않는 Draft 10 Client Library를 사용합니다. Google OAuth 2.0을 사용하여 인증 된 서비스 객체를 구성하는 방법

https://developers.google.com/google-apps/calendar/instantiate

최신 클라이언트 라이브러리

구글-API - 자바 클라이언트 1.12.0 베타입니다. 내가 뽑아 낼 수있는 것부터 Draft 10 Client Library 이후로 많은 것들이 바뀌었고 현재 클라이언트 라이브러리에이 코드를 다시 쓰는 방법을 배울 수 없습니다.

사용되지 않는 코드는 다음과 같습니다.

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse; 
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource; 
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant; 
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl; 

import com.google.api.client.http.HttpTransport; 
import com.google.api.client.http.javanet.NetHttpTransport; 
import com.google.api.client.json.jackson.JacksonFactory; 

import com.google.api.services.calendar.Calendar; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

... 

public void setUp() throws IOException { 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JacksonFactory jsonFactory = new JacksonFactory(); 

    // The clientId and clientSecret are copied from the API Access tab on 
    // the Google APIs Console 
    String clientId = "YOUR_CLIENT_ID"; 
    String clientSecret = "YOUR_CLIENT_SECRET"; 

    // Or your redirect URL for web based applications. 
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 
    String scope = "https://www.googleapis.com/auth/calendar"; 

    // Step 1: Authorize --> 
    String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope) 
    .build(); 

    // Point or redirect your user to the authorizationUrl. 
    System.out.println("Go to the following link in your browser:"); 
    System.out.println(authorizationUrl); 

    // Read the authorization code from the standard input stream. 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("What is the authorization code?"); 
    String code = in.readLine(); 
    // End of Step 1 <-- 

    // Step 2: Exchange --> 
    AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport,  jsonFactory, 
    clientId, clientSecret, code, redirectUrl).execute(); 
    // End of Step 2 <-- 

    GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
    response.accessToken, httpTransport, jsonFactory, clientId, clientSecret, 
    response.refreshToken); 

    Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory); 
    service.setApplicationName("YOUR_APPLICATION_NAME"); 
    ... 
} 
... 

현재 클라이언트 라이브러리에서 작동하도록이 코드를 다시 작성하는 방법을 알려줄 사람이 있습니까?

답변

0

당신이 최신 구글 드라이브 API의 문서를 살펴 가질 수 있습니다

https://developers.google.com/drive/credentials

그런 다음이 일정 범위로 드라이브 범위를 대체하는 하드해서는 안와 서비스 서비스 객체 대신 올바른 Calendar 클래스를 인스턴스화합니다.

그런 상황이 다시 발생하면 최신 버전의 코드 샘플을 찾을 수 있는지 확인하는 것이 Java 용 Google API 클라이언트 라이브러리의 웹 사이트에서 직접 살펴 보는 것입니다. 당신은 그것의 모습을보실 수 있습니다 wiki about auth뿐만 아니라 그들이 최신 버전의 라이브러리 (그것은 모든 문서를 가끔은 최신 상태로 유지하는 것이 더 어렵습니다) 컴파일하고 작업하고 있는지 확인 the sample apps 좀 봐있다

+0

감사합니다 Nivco Logged 귀하의 회신에 대해 배우고 생각할 많은 정보를 제게 제공했습니다. – Laurence

관련 문제