简体   繁体   English

Box API Node.js无法上传文件404错误

[英]Box API Node.js cant upload file 404 Error

I cant upload file with Box API my code: 我无法使用Box API上传代码:

var sdk = new BoxSDK({
  clientID: BOX_clientID,
  clientSecret: BOX_clientSecret
});





// Create a basic API client
var client = sdk.getBasicClient(BOX_accesstoken);



var fileData = fs.createReadStream('C:\\Exports\\test.txt')

client.files.uploadFile('123', '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 + '!');
});

This is the output 这是输出

Hello, UserXYZ! 您好,UserXYZ!
err: Error: Unexpected API Response [404 Not Found] (not_found: "Not Found") 错误:错误:意外的API响应[404未找到](not_found:“未找到”)

Also I really don´t understand how to refresh my token with node.js the code from the docs is not working for me. 而且我真的不明白如何使用node.js刷新令牌,文档中的代码对我不起作用。 Has somebody working sample codes for uploading files and how I can endless use a token I don´t want to enter new access codes everytime. 有某人正在工作用于上传文件的示例代码,以及如何无休止地使用令牌,我不想每次都输入新的访问代码。

The first parameter to client.files.upload needs to be a valid folder_id so I believe that's why the [404 Not Found] appears. client.files.upload的第一个参数必须是有效的folder_id,因此我相信这就是[404 Not Found]出现的原因。 So you can try something like '0'. 因此,您可以尝试类似“ 0”的操作。

Here's an example using jwt and it will take care of the token refresh. 这是一个使用jwt的示例,它将处理令牌刷新。

// --------------- jwt --------------------------
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
    }
});

// --------------- your code ---------------------
var client = sdk.getAppAuthClient('enterprise', ENTERPRISE_ID);

var fileData = fs.createReadStream('C:\\Exports\\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 + '!');
});

Ok, I see what's going on. 好的,我知道发生了什么事。 If you're using Box's generated keys, do the following. 如果您使用Box的生成密钥,请执行以下操作。

  1. create a directory called config 创建一个名为config的目录
  2. rename Box's config file to default.json 将Box的配置文件重命名为default.json
  3. And then the code looks like this... 然后代码看起来像这样...

    var BoxSDK = require('box-node-sdk'); var BoxSDK = require('box-node-sdk'); var config = require('config'); var config = require('config'); var fs = require('fs'); var fs = require('fs'); var path = require('path'); 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 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'),密码:config.get('boxAppSettings.appAuth.passphrase'),expirationTime:60,verifyTimestamp:false}));

    var client = sdk.getAppAuthClient('enterprise', "xxxxx"); 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); 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);
    } }); }};

    // 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 + '!'); }); client.users.get(client.CURRENT_USER_ID,null,function(err,currentUser){if(err)throw err; console.log('Hello,'+ currentUser.name +'!');});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM