簡體   English   中英

使用憑據-Google Drive SDK + Java

[英]Working with credentials - google drive sdk + java

我需要使用Google Drive SDK將文件上傳到Google Drive光盤中。 我有簡單的課程可以做到這一點:

public class DriveApiTest {
    private static String CLIENT_ID = "...";
    private static String CLIENT_SECRET = "...";

    // private static String REDIRECT_URI = "...";
    private static String REDIRECT_URI = "...";

    static HttpTransport httpTransport = new NetHttpTransport();
    static JsonFactory jsonFactory = new JacksonFactory();
    static String parentId = "0B8Myj-AtyvWAWlNTQmpMdWxCRTQ";

    public static void start() throws IOException {
        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);
        try {
            Desktop.getDesktop().browse(new URI(url));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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);

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

        File body = new File();
        body.setTitle("jeszcze inny Plik w podfolderze");
        body.setMimeType("text/csv");
        body.setParents(Arrays.asList(new ParentReference().setId(parentId)));

        java.io.File fileContent = new java.io.File(
                "C:\\Users\\TomekZ\\Desktop\\RoboczeExcele\\Person.csv");
        FileContent mediaContent = new FileContent("text/csv", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());
    }
}

它正在工作,但用戶必須從瀏覽器復制代碼並將其粘貼到控制台。 沒有瀏覽器的復制代碼,是否有可能做到這一點? 我發現了類似的內容(來自: https : //developers.google.com/drive/web/service-accounts

public static Drive getDriveService() throws GeneralSecurityException,
    IOException, URISyntaxException {
  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
      .setServiceAccountScopes(DriveScopes.DRIVE)
      .setServiceAccountPrivateKeyFromP12File(
          new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
      .build();
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential).build();
  return service;
}

但我得到異常:

Exception in thread "main" java.io.IOException: toDerInputStream rejects tag type 56

而且我無法理解並找到問題所在。

使用憑證的操作如此困難,而且我不能編寫非常簡單的應用程序,這讓我感到非常驚訝。

如果使用服務帳戶,則需要在項目中生成“ SERVICE_ACCOUNT_PKCS12_FILE”,需要提供將隨服務帳戶一起生成的SERVICE_ACCOUNT_EMAIL,還需要下載密鑰文件並提供路徑(SERVICE_ACCOUNT_PKCS12_FILE_PATH)。 它非常簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM