我目前遇到的问题似乎无法解决。也许有人可以澄清我做错了什么,或者更好地了解发生了什么。
我在Google云端硬盘中有一个文件/文件夹,在域级别共享(1),只有链接,(2)与某些特定用户共享。在我用Java编写的当前应用程序中,我希望获得当前在此文件/文件夹上设置的所有权限。
显然,我的第一个入口点是测试权限列表调用:Google Developers。 授予以下权限以测试其余调用:
我的通话结果包含授予用户和域(类型)的权限。这是响应:
200 OK
- SHOW HEADERS -
{
"kind": "drive#permissionList",
"etag": "\"xxxxxxxxxx\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/xxxxxxxxxx/permissions",
"items": [
{
"kind": "drive#permission",
"etag": "\"xxxxxxxxxx\"",
"id": "xxxxxxxxxx",
"selfLink": "xxxxxxxxxx",
"name": "Owner Name",
"emailAddress": "owner@random-domain.com",
"domain": "random-domain.com",
"role": "owner",
"type": "user"
},
{
"kind": "drive#permission",
"etag": "\"xxxxxxxxxx\"",
"id": "xxxxxxxxxx",
"selfLink": "xxxxxxxxxx",
"name": "User Name",
"emailAddress": "user@random-domain.com",
"domain": "random-domain.com",
"role": "writer",
"type": "user",
"photoLink": "xxxxxxxxxx"
},
{
"kind": "drive#permission",
"etag": "\"xxxxxxxxxx\"",
"id": "xxxxxxxxxx",
"selfLink": "xxxxxxxxxx",
"name": "Domain Name",
"domain": "random-domain.com",
"role": "reader",
"type": "domain",
"withLink": true
}
]
}
到目前为止一切顺利。在上面的响应中,您可以观察到已返回以下权限:所有者,用户,带链接的域共享。所以现在我试图在我的项目中做同样的事情。
public static void myAwesomeMethod(String fileId) {
Drive service = DriveDirectoryServiceManager.getDriveService("owner@random-domain.com");
PermissionList permissions = service.permissions().list(fileId).execute();
List<Permission> permissionList = permissions.getItems();
...
}
对于想知道DriveDirectoryServiceManager背后发生了什么的人。这是:
public class DriveDirectoryServiceManager {
/** Email of the Service Account */
private static final String SERVICE_ACCOUNT_EMAIL = "xxxxxxxxxx";
private static final String APPLICATION_NAME = "xxxxxxxxxx";
/** Path to the Service Account's Private Key file */
private static final String PKCS = "/xxxxxxxxxx";
/**
* Build and returns a Directory service object authorized with the service
* accounts that act on behalf of the given user.
*
* @return Directory service object that is ready to make requests.
*/
public static Drive getDriveService(String user)
throws GeneralSecurityException, IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
List<String> scope = new ArrayList<>();
scope.add(DriveScopes.DRIVE);
scope.add(DriveScopes.DRIVE_FILE);
scope.add(DriveScopes.DRIVE_APPDATA);
scope.add(DriveScopes.DRIVE_APPS_READONLY);
scope.add(DriveScopes.DRIVE_READONLY);
InputStream keyStream = DriveDirectoryServiceManager.class.getResourceAsStream(PKCS);
PrivateKey key = SecurityUtils.loadPrivateKeyFromKeyStore(
SecurityUtils.getPkcs12KeyStore(), keyStream, "notasecret", "privatekey", "notasecret");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(scope)
.setServiceAccountUser(user)
.setServiceAccountPrivateKey(key)
.build();
return new Drive.Builder(httpTransport, jsonFactory, null).setHttpRequestInitializer(credential).setApplicationName(APPLICATION_NAME).build();
}
}
可以预期permissionList现在包含3个权限,但事实并非如此。似乎只返回2个权限。在域级别上授予的权限不在结果列表中。这是我在调试器中检查它时的输出。
permissionList = {ArrayList@6468} size = 2
0 = {Permission@6472} size = 9
0 = {DataMap$Entry@6481} "domain" -> "random-domain.com"
1 = {DataMap$Entry@6482} "emailAddress" -> "owner@random-domain.com"
2 = {DataMap$Entry@6483} "etag" -> ""xxxxxxxxxx""
3 = {DataMap$Entry@6484} "id" -> "xxxxxxxxxx"
4 = {DataMap$Entry@6485} "kind" -> "drive#permission"
5 = {DataMap$Entry@6486} "name" -> "Owner Name"
6 = {DataMap$Entry@6487} "role" -> "owner"
7 = {DataMap$Entry@6488} "selfLink" -> "xxxxxxxxxx"
8 = {DataMap$Entry@6489} "type" -> "user"
1 = {Permission@6473} size = 10
0 = {DataMap$Entry@6556} "domain" -> "random-domain.com"
1 = {DataMap$Entry@6557} "emailAddress" -> "user@random-domain.com"
2 = {DataMap$Entry@6558} "etag" -> ""xxxxxxxxxx""
3 = {DataMap$Entry@6559} "id" -> "xxxxxxxxxx"
4 = {DataMap$Entry@6560} "kind" -> "drive#permission"
5 = {DataMap$Entry@6561} "name" -> "User Name"
6 = {DataMap$Entry@6562} "photoLink" -> "xxxxxxxxxx"
7 = {DataMap$Entry@6563} "role" -> "writer"
8 = {DataMap$Entry@6564} "selfLink" -> "xxxxxxxxxx"
9 = {DataMap$Entry@6565} "type" -> "user"
到目前为止,我还没有找到结果列表不同的真正原因。
答案 0 :(得分:0)
我找到了问题的根源。尝试此操作后,我发现如果我手动将文件ID和权限ID输入到java权限获取方法中,它确实找到了权限。
在比较驱动器文件夹的创建和驱动器文件夹的更新时,显然在'myAwesomeMethod'中传递了另一个'fileId'值。在这种特殊情况下,我正在使用其余调用检查的文件夹的父文件夹具有完全相同的共享权限,除了域共享,因此这会使数据混乱。
我能给出的唯一提示是始终使用权限本身上的简单 Get()进行检查。这对我来说很有把握,因为它指出了我正确的方向。