API box target
Hey!
I wrote the following code with the Python SDK:
from boxsdk import DevelopmentClient
client = DevelopmentClient()
items = client.folder(folder_id='0').get_items()
for item in items:
print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))
After I entered the token, I got a success response:
GET https://api.box.com/2.0/folders/0/items {'headers': {'Authorization': '---crqY',
'User-Agent': 'box-python-sdk-2.12.0',
'X-Box-UA': 'agent=box-python-sdk/2.12.0; env=python/3.9.4'},
'params': {'offset': 0}}
"GET https://api.box.com/2.0/folders/0/items?offset=0" 200 456
{'Date': 'Tue, 18 May 2021 22:58:38 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store', 'Content-Encoding': 'gzip', 'BOX-REQUEST-ID': '09f109ee5db0ffa8a1cbf9985ffcb9adb'}
{'entries': [{'etag': '0',
'id': '137623996023',
'name': 'afc_permenant',
'sequence_id': '0',
'type': 'folder'},
{'etag': '0',
'id': '137623898981',
'name': 'afc_upload_batch_16114961890_0_1621372798_b5e379e6-fe62-4f8a-bf89-d2941814d077',
'sequence_id': '0',
'type': 'folder'},
{'etag': '0',
'id': '137626139913',
'name': 'My Stuff',
'sequence_id': '0',
'type': 'folder'}],
'limit': 100,
'offset': 0,
'order': [{'by': 'type', 'direction': 'ASC'},
{'by': 'name', 'direction': 'ASC'}],
'total_count': 3}
Folder 137623996023 is named "afc_permenant"
Folder 137623898981 is named "afc_upload_batch_16114961890_0_1621372798_b5e379e6-fe62-4f8a-bf89-d2941814d077"
Folder 137626139913 is named "My Stuff"
However, I don´t have these files in my box. I don't know why I'm receiving that info. Perhaps I have to add some lines in order to define the target.
Could you give me some advices, please?
Thanks.
Hernan M
-
正式なコメント
Hello Hernan!
When the application you created in the Box developer console gets authorized by the administrator, a service account is automatically generated for the application. You can see how to see that login here. The folders you are seeing in your api call are folders that live under that service account. In order to see files and folders under your Box account, you will want to make API calls as your user, like this. There are tons of examples using the python sdk in the usage markdown files here. Feel free to take a look. Let me know if you have additional questions.
Thanks,
Alex, Box Developer Advocate
コメントアクション -
Hi Alex! Thanks for your reply!
I was trying to use the impersonation code:
user_to_impersonate = client.user(user_id='###########')
user_client = client.as_user(user_to_impersonate)
items = user_client.folder(folder_id='0').get_items()
for item in items:
print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))However, I obtain a 403 error:
"GET https://api.box.com/2.0/folders/0/items?offset=0" 403 217
{'Date': 'Thu, 20 May 2021 15:25:48 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store', 'Content-Encoding': 'gzip', 'BOX-REQUEST-ID': '0b904846ce1031762268369f20d55cb5e'}
{'code': '---ions',
'help_url': 'http://developers.box.com/docs/#errors',
'message': 'Access denied - insufficient permission',
'request_id': '4qe048gqpb50616i',
'status': 403,
'type': 'error'}I was trying to get the list of users in order to validate my user_id even I tried to create a new user, but the answer is the same: a 403 error.
Do you know what would be the problem?
Thanks!
Hernan M
-
Hi!
I'm using the Server Authentication for my Custom App.
I've only got a Client ID and the Primary Access Token in order to be authenticated.
I think the image you send is related to a Auth2.0, right?
I would like to upload a file to my Box at the end of my Python script. There is not a GUI, only CLI.
Thanks for your help.
Hernan M
-
Hey Hernan,
It looks like you're making this API call as user ID 16114961890, which is associated with a Limited Access App, which cannot use the as-user header. I recommend using this endpoint to validate who your token is associated with: https://developer.box.com/reference/get-users-me/.
Best,
Kourtney, Box Developer Advocate
-
Hi Kourtney.
I was trying again and I created a Custom app with JWTauth. I submitted a request for authorization in order to use the APIs. They said me that I don't have to active the make API call using as-user header. And I still have the same problem. Now, I´ve got a new service account user and no chance to watch my Box Files.
from boxsdk import JWTAuth, Client
auth = JWTAuth.from_settings_file('config.json')
client = Client(auth)
user_to_impersonate = client.user(user_id='#######')
user_client = client.as_user(user_to_impersonate)
items = user_client.folder(folder_id='0').get_items()
for item in items:
print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))"GET https://api.box.com/2.0/folders/0/items?offset=0" 403 217
{'Date': 'Thu, 27 May 2021 04:47:39 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store', 'Content-Encoding': 'gzip', 'BOX-REQUEST-ID': '024b5943ab030c7d5a3aba0cc1cd548b'}
{'code': '---ions',
'help_url': 'http://developers.box.com/docs/#errors',
'message': 'Access denied - insufficient permission',
'request_id': 'ynse9zgqww164sry',
'status': 403,
'type': 'error'}Traceback (most recent call last):
File "C:\Users\000245815\PycharmProjects\devnet_project\ibm-box-app\box_backup_app.py", line 71, in <module>
for item in items:
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\pagination\box_object_collection.py", line 105, in next
return next(self._all_items)
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\pagination\box_object_collection.py", line 115, in _items_generator
response_object = self._load_next_page()
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\pagination\box_object_collection.py", line 156, in _load_next_page
box_response = self._session.get(self._url, params=params)
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\session\session.py", line 102, in get
return self.request('GET', url, **kwargs)
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\session\session.py", line 158, in request
response = self._prepare_and_send_request(method, url, **kwargs)
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\session\session.py", line 406, in _prepare_and_send_request
self._raise_on_unsuccessful_request(network_response, request)
File "C:\Users\000245815\PycharmProjects\devnet_project\venv\lib\site-packages\boxsdk\session\session.py", line 320, in _raise_on_unsuccessful_request
raise BoxAPIException(
boxsdk.exception.BoxAPIException: Message: Access denied - insufficient permission
Status: 403
Code: access_denied_insufficient_permissions
Request ID: ynse9zgqww164sry
Headers: {'Date': 'Thu, 27 May 2021 04:47:39 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store', 'Content-Encoding': 'gzip', 'BOX-REQUEST-ID': '024b5943ab030c7d5a3aba0cc1cd548b'}
URL: https://api.box.com/2.0/folders/0/items
Method: GET
Context Info: NoneI had a success case when I used the developer token, but it was temporary because the developer token is valid for 60 minutes. Is there a way that the token don't expire?
Thanks
Hernan M
サインインしてコメントを残してください。
コメント
8件のコメント