简体   繁体   中英

upload files to google drive from a java application without user interaction

I am developing a java application where I want the application to upload files to Google drive without any user interaction and after saving the files I want it to be viewed by a real person. Can you help me achieve the above?

Issues I am facing,

Authenticating using application owned accounts: In case of regular account: authenticated using API key but unable to insert files. In case of service accounts: successfully authenticated and inserted files into the drive but unable to be viewed by a real person. Using upload files: I am not sure where to obtain auth_code and how to set it to the header. A sample code would be helpful.

It would be great if you can help me on this.

The absolute easiest way is to install the Google Drive client, then just drop files directly into the Google Drive directory. Google Drive will automatically upload and publish. Done.

Otherwise, you have to go through the Google Drive API to do the upload and the OAuth API to gain an auth_token. The exact way to do that varies by platform. For a native Java application running on Windows, the code will look something like this:

  try {
    GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(
        transport,
        jsonFactory,
        clientSecrets.getWeb().getClientId(),
        clientSecrets.getWeb().getClientSecret(),
        code,
        clientSecrets.getWeb().getRedirectUris().get(0)).execute();
    return buildEmpty().setAccessToken(response.getAccessToken());
  } catch (IOException e) {
    new RuntimeException("An unknown problem occured while retrieving token");
  }

For more information:

https://developers.google.com/drive/web/examples/java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM