Hello my friend! I am trying to retrieve data from statistical api since last week with no success. The error is 400, bad request. I am using firebase cloud functions with javascript. My code is below:
exports.getClearDates = functions.https.onRequest(async (req, res) => {
const client_id = ‘xxx’;
const client_secret = ‘xxx’;
const time_range = ‘2023-01-01/2023-01-30’;
const geometry = {
  "type": "Polygon",
  "coordinates": [
    [
      [-105.021443, 39.578057],
      [-105.021443, 39.618057],
      [-105.041443, 39.618057],
      [-105.041443, 39.578057]
    ]
  ]
};
const tokenConfig = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  }
};
const tokenBody = qs.stringify({
  client_id,
  client_secret,
  grant_type: "client_credentials"
});
let access_token;
try {
  const tokenResponse = await axios.post("https://services.sentinel-hub.com/oauth/token", tokenBody, tokenConfig);
  access_token = tokenResponse.data.access_token;
} catch (error) {
  console.error('Erro ao obter token de acesso:', error);
  return res.status(500).send('Erro ao obter token de acesso');
}
const evalscript = `
  //VERSION=3
  function setup() {
    return {
      input: ["CLM"],
      output: { bands: 1 }
    };
  }
  function evaluatePixel(sample) {
    return [sample.CLM * 255];
  }
`;
const statsRequest = {
  input: {
    bounds: {
      geometry: geometry
    },
    properties: {
        "crs": "http://www.opengis.net/def/crs/EPSG/0/32633"
        },
    data: [{
      type: "sentinel-2-l2a",
    }]
  },
  aggregation: {
    timeRange: {
        from: time_range.split('/')[0] + "T00:00:00Z",
        to: time_range.split('/')[1] + "T23:59:59Z"
      },
    evalscript: evalscript,
    aggregationInterval: {
      of: "P1D"
    }
  }
};
try {
  const response = await axios.post(
    `https://services.sentinel-hub.com/api/v1/statistics`,
    statsRequest,
    {
      headers: {
        'Authorization': `Bearer ${access_token}`,
        'Content-Type': 'application/json', // Corrigido para application/json
        'Accept': 'application/json'
      }
    }
  );
  const clearDates = response.data.data
    .filter(item => item.outputs.default.bands.B0.stats.mean === 0)
    .map(item => item.interval.from);
  res.status(200).send({ clear_dates: clearDates });
} catch (error) {
  console.error('Erro ao buscar dados estatísticos:', error);
  res.status(500).send('Erro ao buscar dados estatísticos');
}
});
Can you take a look and see if there is a mistake?
Thanks so much!
Oswaldo.