Im tryng是一种get方法,我需要通过报头通过身份验证,但它不起作用--一种身份验证是添加密钥:授权和值:“令牌{令牌代码}第二类是通过向标头添加一个带有用户名/密码的基本地址。
var http = require('https');
var header = {
'Authorization': 'Token token code'
}
var optionsget = {
host : 'kc.kobotoolbox.org', // here only the domain name
// (no http/https !)
port : 443,
path : '/api/v1/forms/191797/form.json', // the rest of the url with parameters if needed
method : 'GET' ,
headers: header
};
http.request(optionsget,function(res){
console.log(res);
var body = '';
res.on('data',function(chunk){
console.log(chunk);
body+= chunk;
});
res.on('end', function(){
// var dados = JSON.parse(body);
console.log(body);
})
}).end();第二次尝试
var http = require('https');
var username = 'user';
var password = 'pw';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
var header = {'Host':'kc.humanitarianresponse.info/api/v1/data', 'Authorization': auth};
var optionsget = {
host : 'kc.humanitarianresponse.info', // here only the domain name
// (no http/https !)
port : 443,
path : '/api/v1/forms/191797/form.json', // the rest of the url with parameters if needed
method : 'GET' ,
'Authorization': auth
};
http.request(optionsget,function(res){
console.log(res);
var body = '';
res.on('data',function(chunk){
console.log(chunk);
body+= chunk;
});
res.on('end', function(){
// var dados = JSON.parse(body);
console.log(body);
})
}).end();我得到的错误是没有提供身份验证凭据。
发布于 2022-08-03 15:40:31
下面的例子展示了“如何从NodeJs调用LinkedIn API端点”,因为如果我们从前端调用它,那么就会出现CORS问题。
假设您的API是https://api.linkedin.com/v2/{ServiceName}
这是一个GET请求。
var token = 'your-token';
var options = {
host: 'api.linkedin.com',
port: 443,
path: '/v2/me',
headers: {
'Authorization': 'Bearer ' + token
}
};
request = https.get(options, function(res){
var body = "";
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
//here we have the full response, html or json object
console.log(body);
});
res.on('error', function(e) {
onsole.log("Got error: " + e.message);
});
});https://stackoverflow.com/questions/51291647
复制相似问题