Google Drive API,用于与其他Google帐户用户共享文件

时间:2018-12-14 10:56:20

标签: php google-api google-drive-api file-sharing

我想通过使用Google Drive PHP API将Google Drive文件共享到其他Google帐户用户的电子邮件ID。有时与该人共享文件,他可以在驱动器中查看/读取该文件,但有时我无法与他共享文件。我无法找到这种不一致的原因。 如果文件未共享,则出现以下错误:

An error occurred: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "internalError",
    "message": "Internal Error. User message: \"An internal error has occurred which prevented the sharing of these item(s): 1544784669.1.junkfile.pdf\""
   }
  ],
  "code": 500,
  "message": "Internal Error. User message: \"An internal error has occurred which prevented the sharing of these item(s): 1544784669.1.junkfile.pdf\""
 }
}

2 个答案:

答案 0 :(得分:0)

如果您尝试同时上传文件并与另一个帐户共享,则需要等待几秒钟,然后再执行权限请求。我可以在这样的Android应用程序中(使用Kotlin和RxJava)实现它:

@RequestMapping(value = "/dashboard")
    public ModelAndView home(HttpServletRequest request, HttpServletResponse 
    res, Model model) {
        // Return answer's dictionary from DB to dashboard view
        CompQuest dizRisp = new CompQuest();
        dizRisp.setDizComp(dashDao.getRispEnd());

        Gson gson = new Gson() ;
     // Use Gson dependency to convert hashmap to String

        String strmap = gson.toJson(dizRisp)
        model.addAttribute("dizRisp", strmap);

        return new ModelAndView("dashboard");    
    }


<script>

   $(document).ready(function(){

    var element = JSON.parse('${dizRisp}');

    $.each( element , function( key, value ) {

         console.log(key);
         console.log(value);

    });

   });


</script>

答案 1 :(得分:0)

我已经回答了我自己的问题,将文件上传到驱动器后,我们将获得$ file_google_id,我们必须使用sleep(10)等待10秒钟,然后才可以与任何人共享该文件。

 sleep(10);
    $permission=insertPermission($service,$file_google_id,$email_id,'user','reader');
    function insertPermission($service,$fileId,$value,$type,$role) {

        $newPermission = new Google_Service_Drive_Permission();

        $newPermission->setEmailAddress($value); 
        $newPermission->setType($type);
        $newPermission->setRole($role);

        try {
            return $service->permissions->create($fileId, $newPermission);

        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }
        return NULL;
    }

谢谢