2013年7月26日 星期五

用 Eclipse Java 開發 Google Drive API (基本範例)

主程式:Eclipse  (我是用Indigo版 3.7)
Eclipse外掛:使用 Eclipse 專用的 Google 外掛程式 (最好參考這裡Google Plugin for Eclipse)
程式碼範例:Quickstart: Run a Drive App in Java

首先先建置Eclipse的環境  (建議不要用中文外掛  問題多多)

先到 Help ==> Install New Software

點Add

Name 隨便打  打你看得懂的就好
Location 輸入外掛的網址
外掛的網址請依照你的Eclipse版本  Google Plugin for Eclipse 輸入
在按OK
我是點這三個
在下載安裝的時候  可能會發生錯誤  可以不用理他
安裝完成後  Eclipse 要重開


你會看到你的Eclipse工具列上多了Google的按鈕  (沒看到請回應我  我忘記我是怎麼弄的)

好了...  Eclipse的環境建置好了

新建Java project

程式碼如下   (怕以後原始網頁會改  在此紀錄我所使用的範例)
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class DriveCommandLine {

  private static String CLIENT_ID = "YOUR_CLIENT_ID";
  private static String CLIENT_SECRET = "YOUR_CLIENT_SECRET";

  private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
  
  public static void main(String[] args) throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
   
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
        .setAccessType("online")
        .setApprovalPrompt("auto").build();
    
    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).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(REDIRECT_URI).execute();
    GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
    
    //Create a new authorized API client
    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

    //Insert a file  
    File body = new File();
    body.setTitle("My document");
    body.setDescription("A test document");
    body.setMimeType("text/plain");
    
    java.io.File fileContent = new java.io.File("document.txt");
    FileContent mediaContent = new FileContent("text/plain", fileContent);

    File file = service.files().insert(body, mediaContent).execute();
    System.out.println("File ID: " + file.getId());
  }
}
請依照請況修改你的Class Name

你會發現你import的部分  有一堆找不到lib的部分

這時候請點工具列的Google按鈕
點Add Google APIs

找Drive API在按Finish  (我是用v2版)
完後  你會看到妳的project底下另外多了Drive API的Lib
再回去看程式碼import的部分
紅底線就會不見了  因為有正確匯入的Google API的JAR

接下來要在自己的帳戶開啟API權限
點Services裡面找Drive API  把她打開
再點API Access ==> Create an OAuth 2.0 client ID
Application type裡面 選Installed application
Installed application 裡選Other
再點Create Client ID


會有類似這樣的東西
對應範例程式碼裡面"CLIENT_ID"跟"CLIENT_SECRET"
照上面紅色箭頭修改

範例程式碼最下面有一段
"java.io.File fileContent = new java.io.File("document.txt");"
這個就是要上傳的檔案
例如C:\document.txt就改成"C:\\document.txt"
這樣程式可以Run了

這個範例會在雲端硬碟的根目錄建立一個"My document"的檔案其內容就是document.txt的內容