0

I am using Application Owned Accounts to create a file. However, I would like the ownership of the file to be transfered to other users. The code below works but permission settings are ignored, so I am not the owner of the file. File ownership stays with the Application Owned Account, so files cannot be deleted permanently.

public File uploadEmptyFile(String title, String subFolderName, String mimetype) throws GDriveAccessException {
    File fileMetadata = new File();
    fileMetadata.setTitle(title);
    fileMetadata.setMimeType(mimetype);
    Permission newPermission = new Permission();

    newPermission.setValue("xxx@xyz.com");
    newPermission.setType("user");
    newPermission.setRole("owner");

    fileMetadata.setUserPermission(newPermission);
    if (subFolderName==null){
        fileMetadata.setParents(Arrays.asList(new ParentReference().setId(FOLDER_ID)));
    }else{
        fileMetadata.setParents(Arrays.asList(new  ParentReference().setId(getFileIdByFolderNName(null,subFolderName))));
    }


    File faux=null;
    try {
        faux= getDrive().files().insert(fileMetadata).execute();
        return faux;
    } catch (IOException e1) {
    throw new GDriveAccessException(e1);
    }

  }

Any help in identifying why the permission settings are being ignored would be welcome.

4

2 回答 2

0

好吧,首先,我发现问题在于无法更改常规谷歌应用程序帐户的所有权。您需要拥有高级许可证才能转移文件所有权。

其次,我发现我真正需要的是模仿:

public static Drive getDrive() throws GDriveAccessException {
    if(drive == null) {
        HttpTransport httpTransport = new UrlFetchTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential;
            credential = new GoogleCredential.Builder()
                  .setTransport(httpTransport)
                  .setJsonFactory(jsonFactory)
                  .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                  .setServiceAccountPrivateKey(getPrivateKey())
                  .setServiceAccountScopes(DriveScopes.DRIVE)
                  .setServiceAccountUser("xxx@xyz.com")// Use this property for impersonation
                  .build();
      Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
          .setHttpRequestInitializer(credential).build();
      drive=service;
      return service;
    }
    return drive;
}

如果有关于这个功能的更好的文档,谷歌会很好。

于 2012-12-19T14:51:39.100 回答
0

You need to use permissions.insert as outlined in the sharing and permissions documentation.

于 2012-12-18T23:42:47.190 回答