使用凭据 - google drive sdk + java

时间:2014-07-29 12:24:34

标签: java google-drive-api google-oauth google-oauth-java-client

我需要使用Google Drive SDK将文件上传到Google云端硬盘光盘。我有简单的课程:

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

我无法理解并发现有什么问题。

我真的很惊讶与信誉工作是如此困难,我不能写出非常简单的应用程序。

1 个答案:

答案 0 :(得分:0)

如果您正在使用服务帐户,则需要在项目中生成“SERVICE_ACCOUNT_PKCS12_FILE”,您需要提供将使用服务帐户生成的SERVICE_ACCOUNT_EMAIL,您需要下载密钥文件并提供路径(SERVICE_ACCOUNT_PKCS12_FILE_PATH)。 它很直接。