Reputation: 11
I am totally new to REST API development and I am struggling a lot with just a simple python request. I am trying to get a file from the BOX Development system (similar to Dropbox) and I have been reading the documentation but I still can't figure it out. I'll try to be more specific:
I want to create a python request and get a file from BOX. Doesn't matter which type of file is cuz ideally I would like to get the equivalent JSON file so I can then modify some of the file's attributes. I am printing the response in order to check that I am getting a 200 response message saying that the request was successful. But then when I use json.dumps() I get the typical error of JSONDecodeError: Expecting value: line 1 column 3 (char 0). This is what I have been trying so far:
import json
import requests
import sys
file_id = 678814253283
r = requests.get("https://app.box.com/file/678814253283", {'Authorization': 'Bearer '+ access_token})
print(r)
json_obj = json.dumps(r.json(), indent=4)
print(json_obj)
I suspect that the address is not the proper one as I gave the one that takes me directly to the file instead of the api one --> https://api.box.com/2.0/files/:file_id. The problem is that when I try to use BOX API documentation, from a PDF file I should get the response example shown in here --> https://developer.box.com/reference/get-files-id/. But instead, I get this: Box File - 678814253283 (Test.pdf).
Here is the code I tried with BOX API documentation:
# Import two classes from the boxsdk module - Client and OAuth2
from boxsdk import Client, OAuth2
# Define client ID, client secret, and developer token.
CLIENT_ID = ""
CLIENT_SECRET = ""
ACCESS_TOKEN = ""
# Create OAuth2 object. It's already authenticated, thanks to the developer token.
oauth2 = OAuth2(CLIENT_ID, CLIENT_SECRET, access_token=ACCESS_TOKEN)
# Create the authenticated client
client = Client(oauth2)
file_id = '678814253283'
file_info = client.file(file_id).get()
print(file_info)
I really don't know what am I doing wrong. It seems like it should be something pretty silly but I am totally a beginner here. Not with programming though but I am a beginner in REST API development. Maybe I am misunderstanding a lot of concepts as well...
Thank you so much in advance!
Upvotes: 1
Views: 1957
Reputation: 247
in the first example you are indeed using the web app URL instead of the URL for the API. Your code might work better if you change it to:
import json
import requests
import sys
file_id = 678814253283
r = requests.get("https://api.box.com/2.0/files/678814253283", {'Authorization': 'Bearer '+ access_token})
print(r)
json_obj = json.dumps(r.json(), indent=4)
print(json_obj)
Now, one thing to note there is that this will get you the information about the file (in JSON format), but not the actual file content.
To get the file content you will need to call https://api.box.com/2.0/files/678814253283/content
instead. This will return the binary data of the file, which can then be written to a file.
In many cases, you will want to use our SDK. I highly recommend using the SDK method to download the content of the file:
CLIENT_ID = ""
CLIENT_SECRET = ""
ACCESS_TOKEN = ""
oauth2 = OAuth2(CLIENT_ID, CLIENT_SECRET, access_token=ACCESS_TOKEN)
# Create the authenticated client
client = Client(oauth2)
file_id = '678814253283'
file_info = client.file(file_id).content()
print(file_info)
The difference here is the second last line:
file_info = client.file(file_id).content()
More information about this endpoint is available here:
https://developer.box.com/reference/get-files-id-content/ https://developer.box.com/guides/downloads/file/
Upvotes: 1