Curl + shared link
AnsweredI want to get the shared link of a file on my box account, to store it in a variable. I have found a curl commando to do that (Get link), but I don’t really understand it. How, do you specify from which file you get te link. I use Box Drive, and I have the path to the file.
-
Hi , a web link is not a shared link.
To get a shared link for a file, update the file to enable the shared link for it.
curl -X PUT https://api.box.com/2.0/files/12345 \ -H 'Authorization: Bearer " ' -H 'Content-Type: application/json" ' -d '{ "shared_link": { "access": "open" } }'
https://developer.box.com/reference/put-files-id/#param-shared_link
You can use our SDK to do this as well.
https://developer.box.com/guides/shared-links/create/#Create-Shared-Link-for-File
-
If I understand it good, in the example of , the 'acces token' must be replaced by the API I get from my box account, and the '12345' is the ID of my file. How do I get the ID of the file (with a program). And, if I enabled schared link, how does I get it?
Because, in the end, I want to write script that can send a link to share the file on my box account. Composing a mail is not a problem, but getting the link to share..
-
You can get the file ID a few different ways. Either, you can go to box.com and find the file and open the preview. You can then see the ID of the file in the URL bar:
In this case the part after "file/" is the file ID.
Another way is to find the file ID by using the API. You can traverse the files and folders in your Box account with our APIs.
https://developer.box.com/reference/get-folders-id-items/
https://developer.box.com/guides/folders/bulk/build-folder-tree/
---
By the way, depending on your programming skills you might wan to use our Box CLI instead.
-
Hi , I tried using your advice and set shared_link attribute on an existing file:
curl -X PUT https://api.box.com/2.0/files/*** \ -H 'Authorization: Bearer ' \ -H 'Content-Type: application/pdf"' \ -d '{ "shared_link": { "access": "open" } }'
The request was successful. But then when I check the file stats, for some reason the "shared_link" is set to null, and I cannot create a shared link.
Also, do you know if there is a way to get a shared link for a file using curl? It looks like in the docs there are only four SDK examples.
-
That command doesn't give me any output at all (no errors either)
I tried running this command instead:
curl -X GET https://api.box.com/2.0/files/67***phone number removed for privacy***?fields=shared -H 'Authorization: Bearer '
And the response is:
{"type":"file","id":"67***phone number removed for privacy***","etag":"0","shared_link":null}
-
Interesting. What kind of Enterprise are you using? Are you on a free personal account? Could you try setting the shared link in the web app and then requesting it via the API?
I just tested and this definitely works:
Set the shared link
curl --location --request PUT 'https://api.box.com/2.0/files/123456789' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer [TOKEN]' \ --data-raw '{ "shared_link": { "access": "open" } }'
Get the shared link
curl --location --request GET 'https://api.box.com/2.0/files/123456789?fields=shared_link' --header 'Authorization: Bearer [TOKEN]'
-
We're on Enterprise account.
Unfortunately this did not help. That curl command runs successfully but then I get as a response to the second one.
{"type":"file","id":"***","etag":"1","shared_link":null}
I was able to set he shared link in the web app but then API still reports it as null.
I think I will try to open a ticket with support to investigate further.
-
can you confirm for me that you can create shared links via the web app?
We do have some admin settings to prevent users from creating shared links.
We also have settings to restrict the visibility of shared links.
It might be that if you try the following your API call will work.
curl --location --request PUT 'https://api.box.com/2.0/files/123456789' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer [TOKEN]' \ --data-raw '{ "shared_link": { "access": "collaborators" } }'
This would create a shared link that is only accessible to those who already have access to the file. You can see all the possible values here. https://developer.box.com/reference/put-files-id/#param-shared_link-access
Please sign in to leave a comment.
Comments
11 comments