2014-11-18 14 views
0

로그인 한 사용자의 Google 드라이브에 파일을 저장해야하는 앱을 만들고있어 로그인 한 사용자가 드라이브에 데이터를 저장할 수 있습니다. 이제 코드에서 Google 드라이브 자격증 명 (clientId, clientSecret 및 refreshToken)을 생성하는 방법에 문제가 있습니다. 예제와 마찬가지로 Google 콘솔로 이동하여 요청합니다. 자바 코드를 사용하여 구현할 수 있습니다. 사실 나는 구글 드라이브 광고 데이터베이스 스토리지를 사용해야합니다. 아래 코드를 사용했지만 클라이언트 ID가 필요합니다.Google 드라이브 자격증 명

public class Main 
{ 
    public static void main(String[] args) 
    { 
    String clientId = "..."; 
    String clientSecret = "..."; 

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

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
     httpTransport, 
     jsonFactory, 
     clientId, 
     clientSecret, 
     Arrays.asList(DriveScopes.DRIVE) 
    ) 
     .setAccessType("online") 
     .setApprovalPrompt("auto").build(); 

    String redirectUri = "urn:ietf:wg:oauth:2.0:oob";  
    String url = 
     flow 
     .newAuthorizationUrl() 
     .setRedirectUri(redirectUri) 
     .build(); 

    System.out.println("Please open the following URL in your browser then type the authorization code:"); 
    System.out.println(" " + url); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String code = br.readLine(); 

    GoogleTokenResponse response = 
     flow 
     .newTokenRequest(code) 
     .setRedirectUri(redirectUri) 
     .execute(); 

    GoogleCredential credential = 
     new GoogleCredential() 
     .setFromTokenResponse(response); 

    Drive service = 
     new Drive.Builder(httpTransport, jsonFactory, credential) 
     .build(); 

    ... 
    } 
} 

답변

0
  1. 클라이언트 ID를 얻으려면을, 당신은 링크 https://console.developers.google.com/ 아래에서 구글 개발자 콘솔로 이동 먼저해야합니다.

  2. 프로젝트 생성을 클릭하고 프로젝트 이름을 입력하십시오.

  3. 클릭 API & AUTH, 여기에 자격 증명을 클릭하면 새 클라이언트 ID 링크가 생성됩니다. 여기에서 클라이언트 ID를 얻습니다.