SentinelHub returns 401 after succesfully getting stats

Hello.

I’m using SentinelHub to get NDVI-statistics for geometries. I’m trying to get statistics for 150 geometries. I have a foreach loop to go through them with asynchronous request toSentinelHub. It goes all good, but at some point it returns 401 Unauthorized after 70-100 succesful geometries responses.

Is this a a common issue and how it can be fixed?

Data is fine, I’ve ran it from geometries 1-80 and 80-150 on their own loops and it gets response for all geometries. It’s just I need it to work reliably on getting succesful response with more than 150 geometries.

Thanks in advance. Here is the evalscript if you need it:

const evalscript = `
    //VERSION=3
    function setup() {
      return {
        input: [{
          bands: [
            "B04",
            "B08",
            "dataMask"
          ]
        }],
        output: [
          {
            id: "data",
            bands: 1
          },
          {
            id: "dataMask",
            bands: 1
          }]
      };
    }
    
    function evaluatePixel(samples) { 
        let index = (samples.B08 - samples.B04) / (samples.B08+samples.B04);
        return {
            data: [index, samples.B08, samples.B04],
            dataMask: [samples.dataMask]        
        };
    }
      `;

Hi, a 401 error usually means that your request is unauthorized. I am not sure how long your request is taking but this probably means that your access token expires midway through processing the geometries that you are analysing.

To solve this, you could obtain a new access token every x number of requests that you are running, on the assumption that you are running a single request for each geometry. For larger scale analyses, I would also recommend taking a look at Batch Statistical API. This API was designed to process large numbers of geometries like your use case.

Ok that makes sense. So I just refresh the token at some point and continue.

I’m sure Batch Statistical API would be great, it’s just over priced for my current need.

Thank you for this.