2014-10-28 5 views
0

Google 캘린더를 인증하는 방법을 찾고 있습니다. 지금 우리가 앱을 코딩 한 방법은 인증 URL을 생성하고 사용자가 해당 URL을 탐색하고 '성공 코드'를 복사하여 프로그램에 붙여 넣은 다음 인증 토큰을 처리하도록 입력하는 것입니다.Java : Google 캘린더 인증

나는 그 과정을 자동화하는 방법을 찾고있다. 우리의 앱이 사용자의 브라우저 창에서 생성 된 성공 코드를 직접 읽게하기 위해서. 이렇게하면 사용자가 직접 코드를 복사하여 붙여 넣을 필요가 없습니다.

이러한 기능을 구현하는 방법과이를 위해 사용해야하는 라이브러리 또는 특정 방법을 알려 주시면 감사하겠습니다.

는 사용자가 탐색 할 수있는 URL을 생성하는 방법이며,이 URL 반환

public static String generateNewTokenStep1() { 


     HttpTransport httpTransport = new NetHttpTransport(); 
     JsonFactory jsonFactory = new JacksonFactory(); 

     // Create the authorization code flow manager 
     Set<String> scope = Collections.singleton(CalendarScopes.CALENDAR); 
     String clientId = "_______"; 
     String clientSecret = "__________"; 


     AuthorizationCodeFlow.Builder codeFlowBuilder = new GoogleAuthorizationCodeFlow.Builder(
       httpTransport, jsonFactory, clientId, clientSecret, scope); 
     codeFlow = codeFlowBuilder.build(); 


     String userId = USER_ID; 

     // "redirect" to the authentication url 
     redirectUri = "urn:ietf:wg:oauth:2.0:oob"; 
     AuthorizationCodeRequestUrl authorizationUrl = codeFlow 
       .newAuthorizationUrl(); 
     authorizationUrl.setRedirectUri(redirectUri); 

     return authorizationUrl.toString(); 

    } 

이 구글에 의해 생성 된 성공 코드에 걸리는 방법입니다.

public static String generateNewTokenStep2(String userInput) { 

     String code = userInput; 
     AuthorizationCodeTokenRequest tokenRequest = codeFlow 
       .newTokenRequest(code); 

     tokenRequest.setRedirectUri(redirectUri); 
     TokenResponse tokenResponse = null; 
     try { 
      tokenResponse = tokenRequest.execute(); 
     } catch (IOException tokenRequestFailed) { 
      System.err.println("Token request failed"); 
     } 
     System.out.println(tokenResponse.getAccessToken()); 
     addToDb(tokenResponse.getAccessToken()); 

     return tokenResponse.getAccessToken(); 
    } 
+0

redirect_uri를 사용하여 원하는 포트 또는 웹 서버에 대한 응답을받을 수 있습니까? https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi – luc

답변

0

당신은 Credential를 얻을 수 AuthorizationCodeInstalledApp를 사용하여이 작업을 수행 할 수 있습니다. 다음 샘플 코드는 Google+ command line sample에서 복사됩니다. 사용자가 수동 단계를 수행 할 필요가 없도록 코드를 수신하고 수신하는 LocalServerReceiver을 생성한다는 점을 제외하면 비슷한 인증 흐름을 사용합니다.

// Set up the HTTP transport and JSON factory 
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 

// Load client secrets 
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, 
    new InputStreamReader(PlusSample.class.getResourceAsStream("/client_secrets.json"))); 

// Set up authorization code flow 
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, jsonFactory, clientSecrets, 
    Collections.singleton(PlusScopes.PLUS_ME)).setDataStoreFactory(dataStoreFactory) 
    .build(); 

// Authorize 
Credential credential = 
    new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");