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.