I get a 404 error when I'm trying to download file via Box API every time.
Premise · What I want to realize
I'm trying to download file in Box via Box API, but I get a 404 error every time.
Testing environment
- Box Java SDK 2.24.0
- Scala 2.11.8
- sbt 0.13.13
What I did
Write a code
class BoxFileDownload extends ApiActor with ActorLogging { def receive: PartialFunction[Any, Unit] = { case _event: HttpRequestEvent => implicit val event: HttpRequestEvent = _event // Load the configuration file of the BOX application val reader = new FileReader(".../box/config.json") val boxConfig = BoxConfig.readFrom(reader) // Set a cache information val MAX_CACHE_ENTRIES = 100 val accessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES) // Generate an enterprise connection object of Box Application val client = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, accessTokenCache) // Download the Box file val boxFileId = event.endPoint.getQueryString("box_file_id").getOrElse(0) val file = new BoxFile(client, boxFileId.toString) val info = file.getInfo() val stream = new FileOutputStream(info.getName) file.download(stream) stream.close() } }
Problems occurring · Error messages
When you execute the above code, ...
com.box.sdk.BoxAPIResponseException: The API returned an error code [404 | 7g59i4fxq8w0vd3r] not_found - Not Found at com.box.sdk.BoxAPIResponse.(BoxAPIResponse.java:92) at com.box.sdk.BoxJSONResponse.(BoxJSONResponse.java:32) at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:579) at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:354) at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:329) at com.box.sdk.BoxFile.getInfo(BoxFile.java:452) at jp.co.dac.advr.planning_web.controllers.menu.BoxFileDownload$$anonfun$receive$2.applyOrElse(Index.scala:81) = this line: val info = file.getInfo()
I got an errors. What shoud I do? Could you lend me a hand?
Thank you for reading.
-
you are currently making an API call as the enterprise (we call this a Service Account), not the user who owns the file. You will need to create a client for that user as is described here:
https://github.com/box/box-java-sdk#boxdevelopereditionapiconnectionasenterpriseuser
-
Thank you so much! I solved this problem with your teaching!!
...
...but, I got a new error :_(
At first, the corrected code is here:
class BoxFileDownload extends ApiActor with ActorLogging { def receive: PartialFunction[Any, Unit] = { case _event: HttpRequestEvent => implicit val event: HttpRequestEvent = _event // Load the configuration file of the BOX application val boxConfig = tryWith(getClass.getClassLoader.getResourceAsStream(SharedConf.planning.boxConf)) { boxConf => BoxConfig.readFrom(new InputStreamReader(boxConf)) } // Set a cache information val MAX_CACHE_ENTRIES = 100 val accessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES) // Generate an enterprise connection object of Box Application val USER_ID = SharedConf.planning.boxUserId val api = new BoxDeveloperEditionAPIConnection(USER_ID, DeveloperEditionEntityType.USER, boxConfig, accessTokenCache) // Download the Box file val boxFileId = event.endPoint.getQueryString("box_file_id").getOrElse(0) val file = new BoxFile(api, boxFileId.toString) val info = file.getInfo() val tmpFile = File.createTempFile("box-", "-download") // 一時ファイル val stream = new FileOutputStream(tmpFile) file.download(stream) stream.close() log.debug(s"box download file = ${tmpFile.getPath()}") mkFileDownloadViewFromStream(info.getName, new FileInputStream(tmpFile)) } }
Next, the error is:
com.box.sdk.BoxAPIResponseException: The API returned an error code [403 | p6m06xfxtnkcbelx] access_denied_insufficient_permissions - Access denied - insufficient permission at com.box.sdk.BoxAPIResponse.(BoxAPIResponse.java:92) at com.box.sdk.BoxJSONResponse.(BoxJSONResponse.java:32) at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:579) at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:354) at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:329) at com.box.sdk.BoxFile.download(BoxFile.java:295) at com.box.sdk.BoxFile.download(BoxFile.java:283) at jp.co.dac.advr.planning_web.controllers.menu.BoxFileDownload$$anonfun$receive$2.applyOrElse(Index.scala:89) = this line: file.download(stream)
Could you lend me a hand again?
-
I think you need to enable "As User" in your app.
See https://community.box.com/t5/Platform-and-Development-Forum/Error-access-denied-insufficient-permissions-endpoint-quot-https/td-p/33207 -
Thank you for replying again.
I think I already "As-User" enable my app.
If it's possible, Could you show me the configuration screen of the setting required in this case?
https://dachd.ent.box.com/developers/console/app/APP_ID/configuration
-
If you made a change to the permissions needed, you will need to re-approve the app at the Admin console. See https://community.box.com/t5/How-to-Guides-for-Admins/Enabling-Custom-Built-Applications/ta-p/2188 for details
-
We are running into same issue. we are using oAuth2.0 connection in our App (to connect to Salesforce via Salesforce Box APIs).
We configured the application scope as documented and also added the collaborators. We get the access token, but when trying to access the folder (via backend API),
we get the error 'BoxApiRequest.BoxApiRequestException: The Box API responded with a 404 : Not Found'. Can you help?
Please sign in to leave a comment.
Comments
8 comments