Creating a shared link via API and Curl
AnsweredHi,
In my PHP application, I have uploaded a file to my BOX account and folder. That works great.
I now want to create a shared link I can give to users for this uploaded file.
The following curl returns ok, but the shared_link is still null.
$params['shared_link']['access'] = 'open'; //open|company|collaborators
$params['shared_link']['unshared_at'] = '2020-12-12T10:53:43-08:00';
$response = $client->request('PUT', 'https://api.box.com/2.0/files/12345', [
'headers' => ['Authorization' => "Bearer {$token}"],
'query' => json_encode($params)
])->getBody()->getContents();
return $response;
I'm using Guzzle to wrap the Curl. Everything looks ok and no errors are returned.
But no shared link is being returned in the response. It's still null.
-
I did try the same in Postman with the body as below and confirm that the shared link was created with the expiry date set.
{"shared_link": {"access": "open", "unshared_at": "2020-12-12T10:53:43-08:00"}}
Not sure if you are trying this in a free account as shared link expiry is only available to paid accounts.
:The timestamp at which this shared link will expire. This field can only be set by users with paid accounts."
-
Thank you for you reply. I do have a paid account. Your response gave me the confidence to keep trying.
It turns out I was not forming the parameters right in Guzzle and Curl. In case anybody else needs this, to get it to work in PHP, with Guzzle:
$unshare_date = "2020-12-12T10:53:43-08:00"; $api_url = 'https://api.box.com/2.0/files/' . $fileId; $query = ['fields' => 'shared_link']; $json = ['shared_link' => ['access' => 'open', 'unshared_at' => $unshare_date]]; $params = [ 'headers' => [ 'Authorization' => "Bearer {$token}", ], 'query' => $query, 'json' => $json ]; $res = json_decode($client->request('PUT', $api_url, $params)->getBody()->getContents()); $shared_link = $res->shared_link->url; $download = $res->shared_link->download_url;
-
I have the same issue that you had, did you figure out how to create a curl request to do that?
I am trying this one but it doesn't create a shared link correctly 😞
curl -X PUT https://api.box.com/2.0/files/ \ -H 'Authorization: Bearer ' \ -H 'Content-Type: "application/json"' \ -d '{ "shared_link": { "access": "open" } }'
Please sign in to leave a comment.
Comments
3 comments