You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP+cURL调用Google Drive Rest API创建文件权限报错求助

问题:PHP cURL调用Google Drive API创建权限返回400错误

我尝试通过PHP的cURL调用Google Drive Rest API为文件创建权限,但API始终返回错误。

参考Google Drive官方文档的创建权限示例请求:

POST https://www.googleapis.com/drive/v3/files/FILE_ID/permissions

{
  "requests": [
    {
        "type": "user",
        "role": "commenter",
        "emailAddress": "nancyxxxx@gmail.com"
    }
  ]
}

我尝试了两段代码:

示例代码1

$access_token = 'xxxxxxxxxxxx';
$file_id ='file id goes here';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/$file_id/permissions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $access_token",
    'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  \"requests\": [
    {
        \"type\": \"anyone\",
        \"role\": \"reader\"
    }
  ]
}");

$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

示例代码2

$access_token = 'xxxxxxxxxxxx';
$file_id ='file id goes here';
$headers = array(
"Authorization: Bearer $access_token",
"Content-Type: application/json"
);

$post_fields = '{
  "requests": [
    {
        "type": "anyone",
        "role": "reader"
    }
  ]
}';

$ch = curl_init();                                                       
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/$file_id/permissions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

$response = curl_exec($ch);
var_dump($response);

两段代码均返回如下错误:

{ "error": { "code": 400, "message": "The permission type field is required.", "errors": [ { "message": "The permission type field is required.", "domain": "global", "reason": "required", "location": "permission.type", "locationType": "other" } ] } }

我怀疑请求数据未正确传递至API,或是Google Drive Rest API存在问题,请问该如何解决?


解决方案

你的核心错误是请求格式不符合Google Drive API的接口要求:官方文档里的requests数组是批量修改权限的格式,但你调用的是单个创建权限的接口(/files/{fileId}/permissions),这个接口不需要外层的requests包裹,直接传单个权限对象即可。

另外代码里还有几个细节问题需要修正:

  1. 修正请求体格式
    单个创建权限的接口不需要requests数组,直接传递权限对象:

    {
      "type": "anyone",
      "role": "reader"
    }
    
  2. 修正Content-Type和请求体处理

    • 示例代码1里用了application/x-www-form-urlencoded,但API要求JSON格式,必须改成application/json
    • 建议用json_encode()生成JSON字符串,避免手动转义引号的错误
  3. 移除冗余的cURL参数
    同时设置CURLOPT_POST和CURLOPT_CUSTOMREQUEST为POST是冗余的,保留其中一个即可

以下是修正后的代码:

$access_token = 'xxxxxxxxxxxx';
$file_id = 'file id goes here';

$headers = [
    "Authorization: Bearer $access_token",
    "Content-Type: application/json"
];

// 直接传递单个权限对象,不需要requests数组
$post_data = [
    "type" => "anyone",
    "role" => "reader"
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/$file_id/permissions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取状态码辅助排查
curl_close($ch);

var_dump($http_code, $response);

补充说明:

  • 如果需要批量创建多个权限,应该调用批量修改接口,而不是单个创建接口,批量接口的URL和格式才需要requests数组
  • 确保你的access_token拥有drive.permissions.create的权限范围,否则会返回权限不足的错误
  • 通过curl_getinfo()获取HTTP状态码,能帮助更精准地排查问题

内容的提问来源于stack exchange,提问作者Nancy Moore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 09:10:56