What's the correct way to set file permissions on Google Drive using PHP and cURL?

Hey everyone,

I’m having trouble setting up file permissions on Google Drive using PHP and cURL. I’ve been trying to use the Google Drive API to create permissions, but I keep getting errors.

Here’s what I’ve tried:

$token = 'my_access_token';
$file = 'my_file_id';

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => "https://www.googleapis.com/drive/v3/files/$file/permissions",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer $token",
        'Content-Type: application/json'
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'type' => 'anyone',
        'role' => 'reader'
    ])
]);

$response = curl_exec($curl);
curl_close($curl);

var_dump($response);

But I’m getting an error saying ‘The permission type field is required.’ Am I formatting the request wrong? Or is there something else I’m missing?

Any help would be appreciated. Thanks!

I’ve dealt with similar issues when working with the Google Drive API. The problem often lies in the way data is structured in your CURLOPT_POSTFIELDS. Rather than merging or confusing fields, you should ensure you provide the ‘role’ and ‘type’ fields separately. For instance:

CURLOPT_POSTFIELDS => json_encode([
    'role' => 'reader',
    'type' => 'anyone'
])

Additionally, verify that your access token has the necessary scope (https://www.googleapis.com/auth/drive.file) to create permissions. Implementing error handling can provide further insight:

$response = curl_exec($curl);
if ($response === false) {
    echo 'Curl error: ' . curl_error($curl);
} else {
    $responseData = json_decode($response, true);
    if (isset($responseData['error'])) {
        echo 'API error: ' . $responseData['error']['message'];
    } else {
        echo 'Permission added successfully';
    }
}

This approach should help you narrow down the issue and resolve any errors in your request.

hey man, i think ur close but maybe try adding the ‘transferOwnership’ parameter to ur request. it’s like this:

CURLOPT_POSTFIELDS => json_encode([
‘role’ => ‘reader’,
‘type’ => ‘anyone’,
‘transferOwnership’ => false
])

this worked 4 me when i had similar issues. good luck!

I’ve encountered this issue before, and it’s often related to API versioning. The Drive API v3 requires slightly different parameters compared to v2. Try modifying your request URL to include the ‘supportsAllDrives’ parameter:

CURLOPT_URL => "https://www.googleapis.com/drive/v3/files/$file/permissions?supportsAllDrives=true",

Also, ensure your access token has the correct scope. For file permissions, you typically need ‘https://www.googleapis.com/auth/drive.file’ or ‘https://www.googleapis.com/auth/drive’ depending on your use case.

Lastly, double-check that the file ID is correct and that you have sufficient permissions to modify it. Sometimes, these seemingly small details can cause unexpected errors.