Skip to main content

Hi Team good day, could you help me please



I am trying to read one file into my box Folder Company , but seem I am not able to reach it , it is telling me access denied



var BoxSDK = require(‘box-node-sdk’);



var sdkConfig = require(‘…/Bromont.json’);



var sdk = BoxSDK.getPreconfiguredInstance(sdkConfig);


var client = sdk.getAppAuthClient(‘enterprise’)



var fs = require(‘fs’);



client.users.get(client.CURRENT_USER_ID)


.then(user => console.log(‘Hello’, user.name, ‘!’))


.catch(err => console.log(‘Got an error!’, err));



var client = sdk.getAppAuthClient(‘enterprise’);



client.files.getReadStream(‘1339534847387’, null, function(error, stream) {



const chunks = b];



stream.on('data', function(chunk) {



chunks.push(chunk);



});





stream.on('end', () => {



var result = Buffer.concat(chunks);



var sal=result.toString('base64')



fs.writeFile('newfile.txt', sal, function (err) {

if (err) throw err;

console.log('File is created successfully.');

});





})







});

Hi @user119 , Welcome to the forum!



It seems your are using a JWT authentication, but I’m guessing.



Is the error you’re getting a 404 not found?



If so, and in fact you’re using JWT or CCG authentication, then it is possible that the service user associated with the authentication, does not have access to the file.



You have many options to go around this, depending on how you app security is configured:



Use the as-user header:


The service user can impersonate another user. If your app impersonates the user who owns the file, you will be able to reach it.


Something like this (take a look at our SDK docs)



client.asUser('USER-ID');

client.folders.getItems('0')

.then(items => {

// items contains the collection of files and folders

// in the root folder of the user with USER-ID

});



Another option is to authenticate as the owner user directly with the JWT.


Something like this (take a look at the JWT docs):



Similar to your sample:



var BoxSDK = require('box-node-sdk');

var jsonConfig = require('/path/to/config.json');

var sdk = BoxSDK.getPreconfiguredInstance(jsonConfig);



var serviceAccountClient = sdk.getAppAuthClient('enterprise');



But then get a client as the owner user:



var appUserClient = sdk.getAppAuthClient('user', 'YOUR-APP-USER-ID');



Another option is to have the file owner explicitly share the file with the service account.



A lot of guessing here… Let us know if this helps.



Cheers



PS: I always forget about this, if you change your application configurations, remember to submit for admin approval, and approve it.


Reply