过了一段时间后,我终于弄明白了如何使用oAuth2以及如何使用刷新令牌创建访问令牌。但是我找不到上传文件的node.js样本,我发现这个模块只有https://www.npmjs.com/package/onedrive-api
但这对我不起作用,因为我收到此错误{error:{code:'unauthenticated',message:'Authentication failed'}}
此外,如果我输入accessToken:手动输入'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
另外我只使用了范围Files.readWrite.all我可能需要允许其他一些范围吗?我的代码是
public ActionMethod Esimate(int ID1, int ID2)
{ return Partial View("Some Other View")};
您能否将示例代码发送给我,请使用node.js将文件上传到OneDrive API。会很好。谢谢
编辑:我还尝试使用此
上传文件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
// use the refresh token to create access token
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)
//console.log('temp1: ', temp)
temp = temp.join("")
temp = temp.replace('"access_token":"','')
temp = temp.replace('"','')
temp = temp.replace('\n','')
temp = temp.replace('\r','')
//console.log('temp4: ', temp)
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);
// returns body of https://dev.onedrive.com/items/upload_put.htm#response
})
.catch((err) => {
console.log('Error while uploading File to OneDrive: ', err);
});
} // else from if (err) { from request.post
}); // request.post({ get access token with refresh token
相同代码:'未经验证'的东西:/
答案 0 :(得分:3)
这个示例脚本怎么样?该样本的流程如下。
使用此示例时,请导入文件名,客户端ID,客户端密钥和刷新令牌。详细信息为https://dev.onedrive.com/items/upload_put.htm。
var fs = require('fs');
var mime = require('mime');
var request = require('request');
var file = 'c:/Exports/box.zip'; // Filename you want to upload on your local PC
var onedrive_folder = 'samplefolder'; // Folder name on OneDrive
var onedrive_filename = 'box.zip'; // Filename on OneDrive
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(error, response, body) {
fs.readFile(file, function read(e, f) {
request.put({
url: 'https://graph.microsoft.com/v1.0/drive/root:/' + onedrive_folder + '/' + onedrive_filename + ':/content',
headers: {
'Authorization': "Bearer " + JSON.parse(body).access_token,
'Content-Type': mime.lookup(file),
},
body: f,
}, function(er, re, bo) {
console.log(bo);
});
});
});
如果我误解了你的问题,我很抱歉。