我无法在我成功上传到Google云端硬盘的文件上设置阅读权限。
此外,我想获取URL链接以公开分享。
我不知道为什么我不应该有权更改权限。
An error occurred: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient permissions for this file",
"reason" : "forbidden"
} ],
"message" : "Insufficient permissions for this file"
}
到目前为止我的代码:
public class FilesUploader {
public static void uploadNow(Drive service) {
//...
String fileId = insertFile(service, f.getName(), f.getName());
if (fileId != null) {
List<Permission> permissionList = retrievePermissions(service, fileId);
for (Permission p : permissionList) {
printPermission(service, fileId, p.getId());
updatePermission(service, fileId, p.getId(), "reader");
}
}
}
}
/**
* https://developers.google.com/drive/v2/reference/permissions/update
*/
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;
}
/**
* https://developers.google.com/drive/v2/reference/files/insert
*/
private static String insertFile(Drive service, String title, String filename) {
// File's metadata.
File body = new File();
body.setTitle(title);
// File's content.
java.io.File fileContent = new java.io.File(filename);
FileContent mediaContent = new FileContent("", fileContent);
try {
File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());
return file.getId();
} catch (Exception e) {
e.printStackTrace();
System.out.println("An error occured: " + e);
}
return null;
}
/**
* https://developers.google.com/drive/v2/reference/permissions/get
*/
private static void printPermission(Drive service, String fileId,
String permissionId) {
try {
Permission permission = service.permissions().get(
fileId, permissionId).execute();
System.out.println("Name: " + permission.getName());
System.out.println("Role: " + permission.getRole());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
/**
* https://developers.google.com/drive/v2/reference/permissions/list
*/
private static List<Permission> retrievePermissions(Drive service,
String fileId) {
try {
PermissionList permissions = service.permissions().list(fileId).execute();
return permissions.getItems();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
}
答案 0 :(得分:0)
找到解决方案
insertPermission(service, fileId, "anyone", "reader");
System.out.println( file.getWebContentLink());
private static Permission insertPermission(Drive service, String fileId,
String type, String role) {
Permission newPermission = new Permission();
// newPermission.setValue(value);
newPermission.setType(type);
newPermission.setRole(role);
try {
return service.permissions().insert(fileId, newPermission).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}