3
votes

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 = "[email protected]";


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

1 Answers

4
votes

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.