https://www.npmjs.com/package/onedrive-api
但这对我来说不起作用,因为我得到了这个错误{error:{code:'unauthenticated',消息:'Authentication failed'}}
此外,如果我输入accessToken:manually with“xxxxxxx”,结果将是相同的。
https://dev.onedrive.com/auth/msa_oauth.htm
在这里,您可以生成1h访问令牌,上传功能可以工作。我根据这个问题用awnser创建了我的auth。
OneDrive Code Flow Public clients can't send a client secret - Node.js
此外,我只使用了scope Files.readWrite。我可能需要允许其他范围吗?我的代码是
const oneDriveAPI = require('onedrive-api');
const onedrive_json_configFile = fs.readFileSync('./config/onedrive.json', 'utf8');
const onedrive_json_config = JSON.parse(onedrive_json_configFile);
const onedrive_refresh_token = onedrive_json_config.refresh_token
const onedrive_client_secret = onedrive_json_config.client_secret
const onedrive_client_id = onedrive_json_config.client_id
request.post({
url:'https://login.microsoftonline.com/common/oauth2/v2.0/token',
form: {
redirect_uri: 'http://localhost/dashboard',
client_id: onedrive_client_id,
client_secret: onedrive_client_secret,
refresh_token: onedrive_refresh_token,
grant_type: 'refresh_token'
}
}, function(err,httpResponse,body){
if (err) {
console.log('err: ' + err)
}else{
console.log('body full: ' + body)
var temp = body.toString()
temp = temp.match(/["]access[_]token["][:]["](.*?)["]/gmi)
temp = temp.join("")
temp = temp.replace('"access_token":"','')
temp = temp.replace('"','')
temp = temp.replace('\n','')
temp = temp.replace('\r','')
oneDriveAPI.items.uploadSimple({
accessToken: temp,
filename: 'box.zip',
parentPath: 'test',
readableStream: fs.createReadStream('C:\\Exports\\box.zip')
})
.then((item,body) => {
console.log('item file upload OneDrive: ', item);
console.log('body file upload OneDrive: ', body);
})
.catch((err) => {
console.log('Error while uploading File to OneDrive: ', err);
});
}
});
编辑:我还试着上传一个带有这个的文件
var uri = 'https://api.onedrive.com/v1.0/drive/root:/' + 'C:/files/file.zip' + ':/content'
var options = {
method: 'PUT',
uri: uri,
headers: {
Authorization: "Bearer " + accesstoken
},
json: true
};
request(options, function (err, res, body){
if (err) {
console.log('#4224 err:', err)
}
console.log('#4224 body:', body)
});
相同代码:“未经验证”的内容:/