0

我需要使用 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

我无法理解并找到问题所在。

我真的很惊讶,使用凭证是如此困难,而且我不能编写非常简单的应用程序。

4

1 回答 1

0

如果您使用服务帐户,您需要在项目中生成“SERVICE_ACCOUNT_PKCS12_FILE”,您需要提供服务帐户生成的SERVICE_ACCOUNT_EMAIL,您需要下载密钥文件并提供路径(SERVICE_ACCOUNT_PKCS12_FILE_PATH)。它非常简单。

于 2014-09-24T11:17:01.883 回答