box-node-sdk icon indicating copy to clipboard operation
box-node-sdk copied to clipboard

401 Authorization Error when calling createZip

Open ghowlettgomez opened this issue 3 years ago • 13 comments

  • [x] I have checked that the SDK documentation doesn't solve my issue.
  • [x] I have checked that the API documentation doesn't solve my issue.
  • [x] I have searched the Box Developer Forums and my issue isn't already reported (or if it has been reported, I have attached a link to it, for reference).
  • [x] I have searched Issues in this repo and my issue isn't already reported.

Description of the Issue

Hello, I am currently dealing with an issue where when I attempt to create a Zip Download, the one described in the link below, I get a 401 error when attempting to create it. I am using a preconfigured instance of authentication, and I am able to authenticate myself when trying out all of the other methods.

https://developer.box.com/reference/post-zip-downloads/

Here is a sample of code that can replicate the issue:

// boxProdConfig and boxDevConfig are json configuration files var sdk = BoxSDK.getPreconfiguredInstance(isProd ? boxProdConfig : boxDevConfig) let serviceAccountClient = sdk.getAppAuthClient('enterprise') serviceAccountClient = await connectAs(serviceAccountClient, process.env.SERVICE_ACCOUNT_EMAIL)

// folderId is a given folder id in Box and name is a given download name. const items = [{ type: 'file', id: folderId }] serviceAccountClient.files.createZip(name, items)

When I run the above code, supplying the variables with appropriate values, I get a 401 authorization error even though I do not get it for other endpoints.

Thank you for looking into this.

Steps to Reproduce

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected Behavior

Error Message, Including Stack Trace

Screenshots

Versions Used

Node SDK:

ghowlettgomez avatar Nov 12 '21 00:11 ghowlettgomez

Hi @ghowlettgomez,

Thanks for submitting this Issue! We will take a look and get back to you ASAP!

@arjankowski

arjankowski avatar Nov 12 '21 12:11 arjankowski

Hi @ghowlettgomez,

To be able to create a Zip Download you need to have selected application scope on your app write all files and folders stored in Box in developer console -> Configuration tab. With each change in scope you should submit your app again for admin authorization in Authorization tab.

Can you confirm that you have this checkbox selected?

arjankowski avatar Nov 12 '21 13:11 arjankowski

@arjankowski Thank you for the reply. I can confirm that the checkbox is selected in our account.

ghowlettgomez avatar Nov 12 '21 18:11 ghowlettgomez

@arjankowski I have been able to make this work with the curl API. My problem is that when I access the download_url, I receive a 500 from Box. Could that be related to this issue? If not, who should I ask about it?

ghowlettgomez avatar Nov 12 '21 22:11 ghowlettgomez

When trying to download using the client.files.downloadZip function, I get the following error:

TypeError: Cannot read property 'download_url' of undefined at /Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/box-node-sdk/src/managers/files.ts:2023:25 at bound (domain.js:416:15) at runBound (domain.js:427:12) at tryCatcher (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:547:31) at Promise._settlePromise (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:604:18) at Promise._settlePromise0 (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:649:10) at Promise._settlePromises (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:729:18) at Promise._fulfill (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:673:18) at Promise._resolveCallback (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:489:14) at Promise._settlePromiseFromHandler (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:559:17) at Promise._settlePromise (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:604:18) at Promise._settlePromise0 (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:649:10) at Promise._settlePromises (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:729:18) at Promise._fulfill (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:673:18) at Promise._settlePromise (/Users/gregoryhowlett-gomez/Desktop/nap/box-sync/node_modules/bluebird/js/release/promise.js:617:21)

This may be related to the above errors.

ghowlettgomez avatar Nov 12 '21 23:11 ghowlettgomez

Hi @ghowlettgomez

When you invoke downloadZip API call, under the hood firstly this method asking server to create a zip file and later it's trying to download it. Unfortunately creating a zip files here failed, so it's not possible to read a download_url from response because it's not there (probably because of 401 http status code). In other words if you are unable to successfully call crateZip then same will happen with downloadZip.

I created a tested app to reproduce your case and I'm able to crate a zip file successfully (not received 401 http status code). But I received 404 when trying to download created zip file. So I've already created a ticket to our internal team who is responsible for zip files to check this.

In your case there still a problem with 401 status code which can be caused by configuration. Can you explain what actually does this code in your snippet: serviceAccountClient = await connectAs(serviceAccountClient, process.env.SERVICE_ACCOUNT_EMAIL)

Can you test it again without this line above?

arjankowski avatar Nov 15 '21 11:11 arjankowski

@arjankowski

Thank you for looking into this and for alerting Box about the zipfile issue. Here is the code for connectAs:

    const users: { entries: [{ id: string }] } = await serviceAccountClient.enterprise.getUsers({ user_type: 'all', filter_term: serviceAccount })
    if (users.entries.length > 1) {
      throw new Error('More than one user found')
    } else if (users.entries.length <= 0) {
      throw new Error('No users found, is this the right server?')
    }
    serviceAccountClient.asUser(users.entries[0].id)
    return serviceAccountClient

The code above sets the service account to run as a particular user in our account. When we don't use the code above, we are able to call the createZip function without getting a 401. However, we need to call it for the other functions to work.

ghowlettgomez avatar Nov 15 '21 15:11 ghowlettgomez

@arjankowski I have just tested the zip download and a new error came up. There is a file that downloads now, but the zipfile is empty when I test it. Can you replicate this error?

ghowlettgomez avatar Nov 15 '21 22:11 ghowlettgomez

Hi @ghowlettgomez,

Currently everything is working OK for me and I receiving a correct zip file.

But I'm able to reproduce you error with an empty zip file when specify incorrect array of items that I wanted to be part of the created zip.

This occurs when I do typo with id with file or folder or when I try to reach the resources I don't have access to.

Can you please check that your user is able to list all files and folders that you want to zip by client.folders.getItems method?

arjankowski avatar Nov 16 '21 13:11 arjankowski

@arjankowski I was able to get it working; I talked with the head of box accounts on my side and he was able to sort out an issue with authorization. Thank you for your help on this.

This is a different question, but I have found an issue with the zip export. When I export a folder, all of it's contents except a single zip-subfolder is within the folder within the zipfile, but one of the subfolders is within a folder named '_' in the zip export. Who could I ask in Box about this?

ghowlettgomez avatar Nov 18 '21 00:11 ghowlettgomez

Hi @ghowlettgomez,

Nice that it works!

But according to the zip export problem, I have a question. How do you export the zip file, via the Box SDK API or via the box website?

Does this folder name that was placed inside _ folder has any special characters? Is this possible that you can share this folder name with me?

arjankowski avatar Nov 18 '21 10:11 arjankowski

@arjankowski Thank you for your response. I am currently calling the Box API using a manual request from node, but the error persists if I use the Node API as well. Additionally, when I try to download from the box UI I get the same error. The box folder that I'm trying to copy is called

Bailey Crossing - Test 2021 - FNM -SUPP_underwriting_Monday15November202104_09PM

The subfolder within that that is not being copied into the same location as the other files is:

SECTION 6: INSURANCE POLICY REVIEW DUE DILIGENCE

The export winds up looking like this: Screen Shot 2021-11-18 at 8 55 36 AM

The section 6 folder is within the folder entitled '_'.

If you have any other questions about it please feel free to ask.

ghowlettgomez avatar Nov 18 '21 14:11 ghowlettgomez

Hi @ghowlettgomez

It's looks like this issue is not directly related to our SDK but to our zip service. Do you have access to box support? If so I think it will be the quickest way to resolve this issue. You can post your issue on https://support.box.com/. Can you try it?

@arjankowski

arjankowski avatar Nov 18 '21 17:11 arjankowski

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] avatar Dec 19 '22 19:12 stale[bot]

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box Node SDK and feel free to open another PR/issue at any time.

stale[bot] avatar Dec 26 '22 20:12 stale[bot]