Unauthorized Error when Accessing Sentinel Hub API

Hi, everuone,

I am encountering an issue with accessing the Sentinel Hub API. Despite following the recommended procedures for obtaining an access token, I am receiving a 401 Unauthorized error. Below are the details of the steps I have taken and the error message received.

from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

try:
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=token_url, client_secret=client_secret, include_client_id=True)
print(f"Access token: {token[‘access_token’]}“)
except Exception as e:
print(f"Failed to fetch token: {e}”)

from sentinelhub import SHConfig

config = SHConfig()
config.sh_client_id = client_id
config.sh_client_secret = client_secret
config.instance_id = instance ID
config.sh_base_url = ‘https://sh.dataspace.copernicus.eu
config.sh_token_url = token_url
config.sh_auth_token = token[‘access_token’]

print(f"Config: {config}")
from sentinelhub import BBox, CRS, MimeType, SentinelHubRequest, DataCollection, bbox_to_dimensions

target_coords = [136.67299166, 34.36801666, 136.91402222, 34.25316666] # WGS84
target_bbox = BBox(bbox=target_coords, crs=CRS.WGS84)
res = 10
target_size = bbox_to_dimensions(target_bbox, resolution=res)
print(f"Image shape at {res} m res: {target_size} pixels")

evalscript_test = “”"
//VERSION=3
function setup() {
return {
input: [“B04”, “B03”, “B02”],
output: { bands: 3 }
};
}

function evaluatePixel(sample) {
    return [sample.B04, sample.B03, sample.B02];
}

“”"

test_request = SentinelHubRequest(
evalscript=evalscript_test,
input_data=[
SentinelHubRequest.input_data(
data_collection=DataCollection.SENTINEL2_L2A,
time_interval=(‘2021-01-01’, ‘2021-01-31’),
)
],
responses=[SentinelHubRequest.output_response(“default”, MimeType.TIFF)],
bbox=target_bbox,
size=target_size,
config=config,
)

try:
response = test_request.get_data()
print(“Test request successful”)
except Exception as e:
print(f"Test request failed: {e}")

Here is the error message received when using the obtained token to access the API:
401 Client Error: Unauthorized for url: https://services.sentinel-hub.com/api/v1/process
Server response: {“status”: 401, “reason”: “Unauthorized”, “message”: “You are not authorized! Please provide a valid access token within the header [Authorization: Bearer ] of your request.”, “code”: “COMMON_UNAUTHORIZED”}

I have double-checked the Client ID, Client Secret, and Instance ID in the Sentinel Hub dashboard, and they are all correct. Could you please assist in identifying the issue and providing a solution?

Thank you for your support.

Please double check you are creating your credentials in the correct dashboard. I see you have CDSE as your base URL but the rest of your request uses services.sentinel-hub.com. The two deployments are not interchangeable!

Create CDSE credentials here: Dashboard

Create SH credentials here: Dashboard

Thank you so much for your kind help!
I have identified the issue I am facing with my current setup.
My Sentinel Hub subscription has expired, so I tried to download Sentinel-2 data using credentials created from my Copernicus account.

I was able to successfully download data using my Copernicus account credentials. However, the downloaded files are in SAFE format. I understand that using Sentinel Hub allows downloading data in GeoTIFF format.

Is it possible to download Sentinel-2 data in GeoTIFF format using my Copernicus account?

Yes, Sentinel Hub APIs work the same in the CDSE deployment as they do in the deployment here.

Please consult the docs here, and any further questions should be directed to their community forum.

Thank you so much for your kind advises.
I added some codes that created Sentinel Hub end-point following this site (Documentation - Authentication).
Now it works!
Thank you so so much!!!

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