dropbox-php-sdk icon indicating copy to clipboard operation
dropbox-php-sdk copied to clipboard

Shared link already exists

Open feralheart opened this issue 6 years ago • 1 comments

I testing the shared link function. I generated one, but when I requests the same function I got this error:

{"error_summary": "shared_link_already_exists/.", "error": {".tag": "shared_link_already_exists"}}

How to check if shared link exists and than use that or create a link if it don't exists?

feralheart avatar Jul 06 '18 12:07 feralheart

You can handle this one of two ways. You can check the file metadata for sharing info, or you can just request the link and handle the error.

Checking for sharing info: https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links

$pathToFile = "/someFolder/someFile";
$response = $dropbox->postToAPI("/sharing/list_shared_links", [ "path" => $pathToFile ]);
$data = $response->getDecodedBody();

$data will have a links[0].link_permissions object in it. If there is no share, links will be an empty array.

Handling the error and continuing:

You can just get the share data from the error, then you don't have to make a second call.

$data = "";
try{
    $pathToFile = "/someFolder/someFile";
    $response = $dropbox->postToAPI("/sharing/list_shared_links", [ "path" => $pathToFile ]);
    $data = $response->getDecodedBody();
} catch (DropboxClientException $e) {
    $response = $e->getMessage();
    $dataObj = json_decode($response, $assoc=true);
    $data = $dataObj['error']['shared_link_already_exists']['metadata'];
}
echo json_encode($data);

dval avatar Jan 07 '20 02:01 dval