3

I have followed the examples given on the Google Drive SDK site for Authorization via Service Accounts (https://developers.google.com/drive/service-accounts) and to insert a file (https://developers.google.com/drive/v2/reference/files/insert). I have managed to get it working using the Client ID/Client secret with oauth2 but need automation so want to use the private key.

My issue is I am given a file id, Title, Description & MIME type in return e.g. File ID: %s0B6ysbMIcH3AGWHJPRmZUTVZZMnM, Title: My document, Description: A test document, MIME type: text/plain but the document does -not- exist in Drive and no errors are returned.

I have been work on this for 2 days without success and would really appreciate any assistance. I have looked on-line and the examples I have found are similar to the below. I have tried multiple Google accounts (one a company Google Apps & another a normal gmail account with the same result).

The code (with the account info changed) :

public class AutoGoogleDrive {

private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/home/jsmith/Java/11111111111111111111111111-privatekey.p12";
private static final String SERVICE_ACCOUNT_EMAIL = "1111111111111@developer.gserviceaccount.com";


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_FILE)
  .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 IOException {


    Drive service = null;
    try {
        service = getDriveService();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


//Insert a text file  
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");

// File's content.
java.io.File fileContent = new java.io.File("/home/jsmith/document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
try {
  File file = service.files().insert(body, mediaContent).execute();

  // Uncomment the following line to print the File ID.
   System.out.println("File ID: %s" + file.getId());
  System.out.println("Title: " + file.getTitle());
   System.out.println("Description: " + file.getDescription());
   System.out.println("MIME type: " + file.getMimeType());

} catch (IOException e) {
  System.out.println("An error occured: " + e); 
} 

}
}

Thanks,

Joe Smith

1 Answers1

4

When using service accounts, the inserted file will be added to the application's Drive account for which there's no Drive UI. Those files are only available through the API.

Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
  • Hi Claudio, Thank you. My use case is I run automated performance testing and would like to upload the test results in Google Drive using the API and publish them to the stakeholders. I thought that the API service accounts would be the best framework. From the API docs it mentions "perform any of the other operations described in "Manage Drive Files." I assume this means once the doc is uploaded I can share it with others. Is there any approach you would recommend for this? – user1807924 Nov 08 '12 at 20:44
  • If you share the document with other users it will show up in their Drive UI, is that not the case? – Claudio Cherubino Nov 08 '12 at 20:58
  • 1
    Hi Claudio, Thank you again. I did some research on-line and following the guide for Permissions : insert and now have the sharing working as expected. My issues are resolved now. :) – user1807924 Nov 10 '12 at 00:31
  • I thought any time you create a service account, you impersonate a user in the domain -- then surely the files should show up in that user's Drive UI? – Don Cheadle Apr 28 '15 at 16:19
  • Literally spent a whole day just trying to figure this out. Thanks for the answer. – NobleUplift Nov 25 '15 at 21:32