Creating a shared link via API and Curl

SOLVED
Go to solution
Highlighted
New Contributor

Creating a shared link via API and Curl

Hi,

 

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.

Tags (4)
3 REPLIES 3
Highlighted
Contributor

Re: Creating a shared link via API and Curl

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."

 

 

Highlighted
New Contributor

Re: Creating a shared link via API and Curl

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;
Occasional Contributor

Re: Creating a shared link via API and Curl

@womble 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/<FILE_ID> \
     -H 'Authorization: Bearer <ACCESS_TOKEN>' \
     -H 'Content-Type: "application/json"' \
     -d '{
       "shared_link": {
         "access": "open"
       }
     }'