Join BoxWorks in San Francisco Nov 12-13! Keynotes, product demos, and Box Master Classes. Reserve your spot!

Box API Node.js cant upload file 404 Error

Answered
New post

Comments

3 comments

  • kendomen

    The only thing I changed was the first parameter '123'.  This needs to be a valid folder_id so I put in '0' and then the upload worked.

     

    In this example, I use jwt so the sdk takes care of the token refresh.

     

    var BoxSDK = require('box-node-sdk');
    var fs = require('fs');
    var path = require('path');
    
    var CLIENT_ID = 'xxxx',
      CLIENT_SECRET = 'xxxx',
      PUBLIC_KEY_ID = 'xxxx',
      PRIVATE_KEY_PATH = 'xxxx',
      PRIVATE_KEY_PASSPHRASE = 'xxxx',
      ENTERPRISE_ID = 'xxxx';
    
    var sdk = new BoxSDK({
        clientID: CLIENT_ID,
        clientSecret: CLIENT_SECRET,
        appAuth: {
          keyID: PUBLIC_KEY_ID,
          privateKey: fs.readFileSync(path.resolve(__dirname, PRIVATE_KEY_PATH)),
          passphrase: PRIVATE_KEY_PASSPHRASE
        }
    });
    
    var client = sdk.getAppAuthClient('enterprise', ENTERPRISE_ID);
    
    var fileData = fs.createReadStream('/users/kdomen/Downloads/test.txt')
    client.files.uploadFile('0', 'test.txt', fileData, function(err, file) {
    if (err){console.log('err: ' + err);
    }
    else{console.log('file uploaded: ' + file);  
    }
    });
    
    
    // Get some of that sweet, sweet data!
    client.users.get(client.CURRENT_USER_ID, null, function(err, currentUser) {
      if(err) throw err;  console.log('Hello, ' + currentUser.name + '!');
    });
    0
    Comment actions Permalink
  • t33n

    Ah I think you wrote me also on stackoverflow 😄 lets quit this chat here and write there pls. Because I already update the status there.
    Thank you

    https://stackoverflow.com/questions/45396100/box-api-node-js-cant-upload-file-404-error

    0
    Comment actions Permalink
  • kendomen

    When using Box's generated keys, I do the following:

    1.  create a directory called config

    2.  rename the config file to default.json

    3.  and then the code changes below

     

    var BoxSDK = require('box-node-sdk');
    var config = require('config');
    var fs = require('fs');
    var path = require('path');
    
    
    var sdk = new BoxSDK({
        clientID: config.get('boxAppSettings.clientID'),
        clientSecret: config.get('boxAppSettings.clientSecret'),
        appAuth: {
          keyID: config.get('boxAppSettings.appAuth.publicKeyID'),
          privateKey: config.get('boxAppSettings.appAuth.privateKey'),
          passphrase: config.get('boxAppSettings.appAuth.passphrase'),
          expirationTime: 60,
          verifyTimestamp: false
        }
    });
    
    var client = sdk.getAppAuthClient('enterprise', "xxxxx");
    
    var fileData = fs.createReadStream('/users/kdomen/Downloads/test.txt')
    client.files.uploadFile('0', 'test.txt', fileData, function(err, file) {
      if (err){
        console.log('err: ' + err);
      }
      else {
        console.log('file uploaded: ' + file);  
      }
    });
    
    
    // Get some of that sweet, sweet data!
    client.users.get(client.CURRENT_USER_ID, null, function(err, currentUser) {
      if(err) throw err;  console.log('Hello, ' + currentUser.name + '!');
    });

     

    0
    Comment actions Permalink

Please sign in to leave a comment.