java - 从 Google 服务帐户创建文件权限时获取 "You can not share this item because it has been flagged as inappropriate"

标签 java spring-boot google-drive-api google-developers-console google-drive-shared-drive

我正在使用 Google 服务帐户将文件上传到 Google 云端硬盘。文件正在成功上传。 但我需要与一些利益相关者分享。因此,在为其添加权限时,我收到此错误:“message”:“错误请求。用户消息:“您无法共享此项目,因为它已被标记为不适当。”

我的代码片段是:

// create and return credential
private static Credential getCredentials2() throws IOException {
    
    java.io.File serviceAccountCredsFile = new java.io.File("creds.json");
    @SuppressWarnings("deprecation")
    GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(serviceAccountCredsFile))
            .createScoped(SCOPES);
    
    return credential;
}

// build and return an authorized drive client service
public static List<File> getDriveService() throws Exception {
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

    // Instantiating a client
    Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials2())
                .setApplicationName(APPLICATION_NAME)
                .build();
    
    System.out.println("Successfully created the Drive service!!!!!!!!!!!!!!!!!!!!");

    
    /***************************Uploading file on Google Drive Start*********************************/
    File fileMetadata = new File();
    fileMetadata.setName("1stFile.csv");
    fileMetadata.setParents(Collections.singletonList("10Tt3rfkaeHr1JOUQYRPTfqQQ2jdDJcy")); 
    fileMetadata.setDescription("Testing upload");
    fileMetadata.setMimeType("application/csv");        
    
    java.io.File fileTobeUploaded = new java.io.File("custid.csv");
    
    FileInputStream inputStream = new FileInputStream(fileTobeUploaded);
    InputStreamContent mediaContent = new InputStreamContent("application/csv", inputStream);

    try {
      File uploadedFile = service.files().create(fileMetadata, mediaContent)
                                .setSupportsAllDrives(true) 
                                .execute();
com.google.api.services.drive.model.Permission newPermission = new com.google.api.services.drive.model.Permission() ;
      newPermission.setType("user");
      newPermission.setRole("writer");
      newPermission.setEmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="344c4d4e744c4d4e1a575b59" rel="noreferrer noopener nofollow">[email protected]</a>");
        
      service.permissions().create(createdFileId, newPermission)
                .setSupportsAllDrives(true)
                .execute(); 

执行“service.permissions().create()”后,出现以下错误:

400 错误请求 发布https://www.googleapis.com/drive/v3/files/16-wFWifo0HNZW6W1lujJiT1HO-1wLbK/permissions?supportsAllDrives=true { “代码”:400, “错误”:[ { “域”:“全局”, "message": "错误的请求。用户消息: "您无法共享此项目,因为它已被标记为不适当。"", “原因”:“无效共享请求” } ], "message": "错误的请求。用户消息: "您无法共享此项目,因为它已被标记为不适当。"" }

确实需要这方面的投入。提前致谢!

最佳答案

尝试设置sendNotificationEmail创建文件权限时设置为 false。尽管我不太确定最初导致错误的原因,但这为我解决了这个问题。可能与服务帐户的设置有关,但我只是猜测。根据documentation发生该错误的原因有多种,但没有明确提及此特定消息的解决方案。

希望这对您有帮助!

关于java - 从 Google 服务帐户创建文件权限时获取 "You can not share this item because it has been flagged as inappropriate",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74098767/

相关文章:

java - 我的舞台在 while 循环中没有响应

java - 为什么我们需要先将 double 转换为字符串,然后才能将其转换为 BigDecimal?

rest - SpringBoot RestController 为路径 'services/*' 返回 405

java - 如何在 IDE 中将应用程序作为 jar 运行,但将 maven 打包为 war?

javascript - 如何发送带有 Blob 的 http 多部分 POST 请求?

node.js - 使用 Google Drive API V3 需要的最低 NodeJs 版本是多少?

java - Spring Batch 多行记录 PatternMatch BeanList

java - 如何从Python中的可执行jar中捕获信息?

java - 如何在 Spring Boot 2.0.5 应用程序上启用 HTTPS

ios - 如何首先按文件夹、名称、大小、日期对 GTLQueryDrive 项目进行排序