简体   繁体   中英

Upload file to google drive in java without OAuth

I want upload a file to my account in google drive. This process should be without human intervention. Is there any way to upload a file in my google drive using the google drive api providing my user and my password?

What I've done so far:

  • I created a project in https://console.developers.google.com and enable the google drive api
  • In https://console.developers.google.com -> APIs & Auth -> Credentials I created a new client id, in application type I selected Service Account and I downloaded the pkcs12 key.
  • I have the next code:

     /** * Email of the Service Account */ private static final String SERVICE_ACCOUNT_EMAIL = "account_of_service_account@developer.gserviceaccount.com"; /** * Path to the Service Account's Private Key file */ private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/path/to/pkcs12/donloaded.pkcs12"; /** * Build and returns a Drive service object authorized with the service * accounts. * * @return Drive service object that is ready to make requests. */ public static Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); List<String> scopes = new ArrayList<String>(); scopes.add(DriveScopes.DRIVE); GoogleCredential credential; credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) .setServiceAccountScopes(scopes) .setServiceAccountPrivateKeyFromP12File(new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)) .build(); Drive service = new Drive.Builder(httpTransport, jsonFactory, null) .setHttpRequestInitializer(credential).build(); return service; } public static void main(String[] args) throws GeneralSecurityException, IOException, URISyntaxException { Drive client = getDriveService(); File body = new File(); body.setTitle("mytitle"); body.setMimeType("application/vnd.google-apps.spreadsheet"); File file = client.files().insert(body).execute(); System.out.println(file.getId()); System.out.println(file.getAlternateLink()); } 

When I run the code the result is ok, I get file's id and the alternate link, but the file not load to my account. I paste the alternate link in a browser but I not have permission.

Where is the file you just uploaded? What should I change to indicate that up to my account drive? Where should I add my username and password to be associated with my drive? What should I do to the file to become public?

Thank you very much for all your attention. I wish they can help me.

You will also need to set a parent on the item, such as 'root' so that they appear someone in the 'My Drive' space. 'root' as a parent is 'My Drive' in the UI.

Use Search in the Drive UI to confirm and you should find they have been uploaded, but are currently in a state called 'unparented' since your code doesn't add a parent.

You are uploading the file into your service account. If you need to access the file, it must be shared with your email. use the following method to share the file with your email.

 public static void shareFileOrFolder(Drive service, File file)
        throws IOException {
    Permission newPermission = new Permission();

    newPermission.setEmailAddress("xxxxx@gmail.com");
    newPermission.setValue("xxxxx@gmail.com");
    newPermission.setType("user");
    newPermission.setRole("writer");
    service.permissions().insert(file.getId(), newPermission).execute();
}

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