I'm trying to write a file on my Google Drive.
This is my code:
/**
* Update a permission's role.
*
* @param service Drive API service instance.
* @param fileId ID of the file to update permission for.
* @param permissionId ID of the permission to update.
* @param newRole The value "owner", "writer" or "reader".
* @return The updated permission if successful, {@code null} otherwise.
*/
private static Permission updatePermission(Drive service, String fileId,
String permissionId, String newRole) {
try {
// First retrieve the permission from the API.
Permission permission = service.permissions().get(
fileId, permissionId).execute();
permission.setRole(newRole);
return service.permissions().update(
fileId, permissionId, permission).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Drive service = getDriveService();
// File's metadata.
File file = new File();
file.setId(??);
file.setTitle("Title");
file.setDescription("Descrizione");
file.setMimeType("application/vnd.google-apps.drive-sdk");
updatePermission(service, file.getId(), ??, "writer");
file = service.files().insert(file).execute();
}
Like you can see, in the Main I'm setting the file properties. But I don't know how do it.
1) What is file id? In file.setId("");
if I set, for example, "1" gradle tell me:
An error occurred: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"location" : "file",
"locationType" : "other",
"message" : "File not found: 1",
"reason" : "notFound"
} ],
"message" : "File not found: 1"
}
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at DriveQuickStart.main(DriveQuickStart.java:152)
:run FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/local/jdk1.7.0_80/bin/java'' finished with non-zero exit value 1
So, what is file id and how can I generate it?
2) In updatePermission what is the id to set?
3) I want to upload a file that I have on my desktop, how can I set the path to this file?
Thanks.