401 error with Statistic API

Hello,
I am using the Statistic API for the first time and I am having a 401 error using one of the examples on the documentation. I am trying tokens that work correctly with the Catalog API. My problem is not related to the expiration of the token because, when that happens, I get a different error. I include my code below in case someone could help me. Thanks!

evalscript = """
//VERSION=3
function setup() {
  return {
    input: [{
      bands: [
        "B04",
        "dataMask"
      ]
    }],
    output: [
      {
        id: "output_B04",
        bands: 1,
        sampleType: "FLOAT32"
      },
      {
        id: "dataMask",
        bands: 1
      }]
  }
}
function evaluatePixel(samples) {
    return {
        output_B04: [samples.B04],
        dataMask: [samples.dataMask]
        }
}
"""

stats_request = {
  "input": {
    "bounds": {
      "bbox": [414315, 4958219, 414859, 4958819],
    "properties": {
        "crs": "http://www.opengis.net/def/crs/EPSG/0/32633"
        }
    },
    "data": [
      {
        "type": "sentinel-2-l2a",
        "dataFilter": {
            "mosaickingOrder": "leastRecent"
        },
      }
    ]
  },
  "aggregation": {
    "timeRange": {
            "from": "2022-11-04T00:00:00Z",
            "to": "2022-11-05T00:00:00Z"
      },
    "aggregationInterval": {
        "of": "P1D"
    },
    "evalscript": evalscript,
    "resx": 10,
    "resy": 10
  }
}

headers = {    
  'Authorization': f'Bearer ${token}',
  'Content-Type': 'application/json',
  'Accept': 'application/json'
}

url = "https://services.sentinel-hub.com/api/v1/statistics"
response = oauth.request("POST", url=url , headers=headers, json=stats_request)
sh_statistics = response.json()
sh_statistics
{'error': {'status': 401,
  'reason': 'Unauthorized',
  'message': 'You are not authorized! Please provide a valid access token within the header [Authorization: Bearer <accessToken>] of your request.',
  'code': 'COMMON_UNAUTHORIZED'}}

Hi Eva,

I’m unable to replicate the issue that you having here. Checking your account, it looks like you do not have an oauth client. You can create this in your Dashboard as described here.

Before sending your Statistical API request, you should set this up as described in the documentation.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.