0

I need to upload file into Google Drive disc using Google Drive SDK. I have simple class to do it:

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());
    }
}

It is working but user have to go to copy code from browse and paste it into console. Is there any possibility to make it without copy code from browse? I found something like this (from: 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;
}

but I'm geting Exception:

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

and I can not understand and find what is wrong.

I am really surprised that working with credintials is so difficult and I can not write very simple application.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jacob
  • 1
  • 3

1 Answers1

0

If you are using service accounts you need to generate "SERVICE_ACCOUNT_PKCS12_FILE" in your project,you need to provide SERVICE_ACCOUNT_EMAIL which will be generated with service account and you need to download key file and provide the path (SERVICE_ACCOUNT_PKCS12_FILE_PATH). Its very straightforward.

Sastry
  • 59
  • 6