I am having some trouble uploading my files using Google Drive API. The API does not return any error and shows as if the file was successfully uploaded. However, when I try to retrieve a list of file it does not appear nor does it appear when I try to look at the Google Drive API page.
Here are some observations:
Here is how my code looks (I've tried to create it based on the Java snippet).
public class Quickstart {   
/** Application name. */
private static final String APPLICATION_NAME = "Drive API Java Quickstart";
/** Directory to store user credentials for this application. */
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/**
 * Global instance of the scopes required by this quickstart.
 *
 * If modifying these scopes, delete your previously saved credentials at
 * ~/.credentials/drive-java-quickstart
 */
private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE);
static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}
/**
 * Creates an authorized Credential object.
 * 
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    GoogleCredential credential = GoogleCredential.fromStream(Quickstart.class.getResourceAsStream("/secret.json"));
    credential = credential.createScoped(SCOPES);
    return credential;
}
/**
 * Build and return an authorized Drive client service.
 * 
 * @return an authorized Drive client service
 * @throws IOException
 */
public static Drive getDriveService() throws IOException {
    Credential credential = authorize();
    return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}
public static String generateFolder(Drive service) throws IOException {
    File fileMetadata = new File();
    fileMetadata.setName("Invoices");
    fileMetadata.setMimeType("application/vnd.google-apps.folder");
    File file = service.files().create(fileMetadata).setFields("id").execute();
    System.out.println("Folder ID: " + file.getId());
    return file.getId();
}
public static void uploadFile(Create createFile, Drive service) throws IOException, InterruptedException {
    File file = createFile.execute();
    Thread.sleep(3000);
    MediaHttpUploader uploader = createFile.getMediaHttpUploader();
    System.out.println(uploader.getProgress());
    System.out.println("File ID: " + file.getId());
    System.out.println(service.files().list());
}
public static Create createFile(String path, Drive service) throws IOException {
    String folderId = generateFolder(service);
    File fileMetadata = new File();
    fileMetadata.setName("photo.jpg");
    fileMetadata.setParents(Collections.singletonList(folderId));
    java.io.File filePath = new java.io.File(path);
    FileContent mediaContent = new FileContent("image/jpeg", filePath);
    Create createFile = service.files().create(fileMetadata, mediaContent).setFields("id, parents");
    return createFile;
}
public static void main(String[] args) throws IOException, InterruptedException {
    Drive service = getDriveService();
    String path = "F:/workspace/drive/files/photo.jpg";
    Create createdFile = createFile(path, service);
    uploadFile(createdFile, service);
}
}
The result of this code is:
Folder ID: "String of the folder ID"
1.0
File ID: "String of the file ID"
{}
(where {} should be all files present on drive)
Anyone has any idea why it is not appearing on the drive?
You appear to be authenticating using a service account. You need to remember that a service account is not you, think of it as a dummy user it has its own Google drive account. You have been uploading files to the service accounts Google drive account. You can do a files.list to see these uploaded files. There is no web interface view for a service account.
You can take the service account email address and share a directory on your personal google drive account this will give the service account permissions to upload to this directory. You will need to remember to have the service account patch the files to grant your user access to the files or you wont have any permissions on them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With