0
Access to Apis should be done by passing the following data in the request header:
"[{"key":"chave-api-dados","value":"123456789"}]
Please! Where do I add these values for authentication and access the API?
function buscarPorCpf(){
const cpf = document.getElementById("cpf").value;
const mesAno = buscaMes();
const url = "http://www.transparencia.gov.br/api-de-dados/bolsa-familia-disponivel-por-cpf-ou-nis?codigo="+cpf+"&anoMesReferencia="+mesAno+"&anoMesCompetencia="+mesAno+"&pagina=1";
var ajax = createCORSRequest('GET', url);
if (!ajax) {
throw new Error('CORS not supported');
}
ajax.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200){
console.log(this.responseText);
if (this.responseText.length < 3) {
$('#dados2').hide();
$('#novaconsulta').show();
}
let resposta = JSON.parse(this.responseText)[0];
const valor = resposta.valor;
const nome = resposta.titularBolsaFamilia.nome;
const cpf = resposta.titularBolsaFamilia.cpfFormatado;
document.getElementById("pcpf").innerHTML = cpf;
document.getElementById("pessoas").innerHTML = nome;
document.getElementById("pvalor").innerHTML = valor;
}
};
ajax.send();
}
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
You included your key in the previous question and I did a test and it is not valid. Check that it is correct and run a test on http://www.portaltransparencia.gov.br/swagger-ui.html. It should be accurate
– tvdias