box / box-python-sdk

Box SDK for Python
http://opensource.box.com/box-python-sdk/
Apache License 2.0
418 stars 215 forks source link

wrong URL path to FOLDER #741

Closed jcreem1 closed 2 years ago

jcreem1 commented 2 years ago

Description of the Issue

-Trying to call get_folder = client.folder(folder_id = 'folder_id').get() and getting a 404 response. -The GET response in the error indicates the URL to the folders sub directory but it lists it as the following - I mask a few things to prevent identification: -"GET [https://[my domain].app.box.com/folders/[folder_id]" 404 0

Expected Behavior

Error Message, Including Stack Trace

-"GET [https://[my domain].app.box.com/folders/[folder_id]"](https://[my domain].app.box.com/folders/[folder_id]%22) 404 0 {'Date': 'Fri, 10 Jun 2022 15:48:06 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Strict-Transport-Security': 'max-age=31536000'} b''

Screenshots

Versions Used

Python SDK: boxsdk==3.3.0 Python: Python 3.10.4

mwwoda commented 2 years ago

HI @jcreem1 Thanks for reaching out to us!

I see in your stacktrace that the SDK tried to call https://[my domain].app.box.com which is rather unusual, as by default the sdk uses the following base url https://api.box.com/2.0 Have you changed the SDK's default BASE_API_URL address (or other configuration)? This could be the reason why you are getting a 404 response.

Is this causing the response to fail? If so, is there an adjustment in the source code I can make to call /folder/ not /folders/?

No. /folders/:folderId endpoint in the API returns information about the given folder. You could see the example here https://developer.box.com/reference/get-folders-id/. You could also try the cURL example on this website to see if the problem is with the SDK itself.

curl -i -X GET "https://api.box.com/2.0/folders/:folderId" \ -H "Authorization: Bearer <ACCESS_TOKEN>"

Hope this helps

jcreem1 commented 2 years ago

Hi there,

Thanks for the response. The https://[my domain].app.box.com is correct. The domain references my company's working Box account.

To add further context I can run client.get_url('folder/[folder_id]') and a hyperlink of

https://[my domain].app.box.com/folder/[folder_id]

is returned that takes me directly to my Box web app folder. The key here is that I supply the "folder/" in the get_url() where as client.folder(folder_id=[folder_id]) will use "folders/" which is incorrect in my case.

I figured out a way around this issue by running the following syntax:

json_response = client.make_request('GET', client.get_url('folder/[folder_id') ,).json()

However, it fails and the following exception is displayed:

BoxAPIException: Message: Non-json response received, while expecting json response. Status: 200 Code: None Request ID: None

As expected the URL that prints out takes me directly to my Box folder.

Any idea what this BoxAPIException message is supposed to convey? I add .json() to the make_request() like it says in the README file on github. I think the request goes through since the return status is 200.

Thanks

antusus commented 2 years ago

Hi @jcreem1,

I understand that you have your custom domain. However i think https://[my domain].app.box.com/folder/[folder_id] is only for Box web application. If you want to call API than you should refer to: https://developer.box.com/reference/ There you could see that you are using wrong URL to access folder. If you haven't changed base URL of the client than your code

client.get_url('folder/[folder_id')
,).json()

will call https://api.box.com/2.0/folder/:folder_id - which is wrong. You can check that with using printing: print(client.get_url('folder/[folder_id')) This will return URL generated using client settings.

The proper URL to access api should be: https://api.box.com/2.0/folders/:folder_id

client.get_url('folders/0')
,).json()

I cannot reproduce getting 200 error. That is why we would like you to share information on how are you setting up your client. You can replace your domain with any string byt please show us what you are doing.

jcreem1 commented 2 years ago

Hi @antusus ,

Thanks again for your responses. Turns out the original problem I was having stemmed from the fact that I needed to invite the Box Service Account as a collaborator in the folder. Similar to the issue discussed in the following post.

https://support.box.com/hc/en-us/community/posts/360049158474-Sharing-box-app-as-collaborator-to-folder

If needed, instructions on how to locate the Service Account can be found here:

https://developer.box.com/guides/getting-started/user-types/service-account/

Prior to finding this solution, I changed the BASE_API_URL variable to my company's domain, therefore, when I ran make_request() the URL returned a working link to my web app and I think triggered the status code 200.

Thanks again