John Davidson

php - next cloud ocs create share api permission issue

0 comments
Message:


while creating share link with the help of ocs share api i am able to create the link but it is not setting the permission that i am trying to give it.


        $ch = curl_init("https://servername/ocs/v2.php/apps/files_sharing/api/v1/shares");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', "OCS-APIRequest:true"));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, getenv('NEXTCLOUDUSERNAME') . ":" . getenv('NEXTCLOUDPASSWORD'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);


My Answer:
To set the permission while creating a share link using the Nextcloud OCS API, you need to include the permission parameter in the POST request data. Here is an example of how you can set the permission while creating a share link:

php
$ch = curl_init("https://servername/ocs/v2.php/apps/files_sharing/api/v1/shares");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', "OCS-APIRequest:true"));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, getenv('NEXTCLOUDUSERNAME') . ":" . getenv('NEXTCLOUDPASSWORD'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

// Set the POST data including the permission parameter
$postData = array(
'path' => '/path/to/file',
'shareType' => 3, // 3 for public link
'permissions' => 1, // 1 for read-only permission
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);


In this example, the `permissions` parameter is set to `1` which corresponds to read-only permission. You can change this value to `0` for read-write permission or any other valid permission value according to your requirements.

Make sure to replace `/path/to/file` with the actual path to the file you want to share. Also, ensure that you have the necessary permissions to set the share permissions using the API.

After making these changes, try running the code again to create a share link with the desired permission.

Rate this post

3 of 5 based on 8648 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us