Sentinel 2 python access

Hi

I’m trying to get some simple data from sentinel 2 dataset. Just copied this simple request script, but it still wants me to put some headers and I don’t know where to do it.

Script looks like this:

from sentinelhub import CRS, BBox, MimeType, SentinelHubRequest, SHConfig
from sentinelhub import DataCollection
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

Write your credentials here if you haven’t already put them into config.toml

CLIENT_ID = “…”
CLIENT_SECRET = “…”

config = SHConfig()
if CLIENT_ID and CLIENT_SECRET:
config.sh_client_id = CLIENT_ID
config.sh_client_secret = CLIENT_SECRET
config.sh_token_url = “https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token
config.sh_base_url = “https://sh.dataspace.copernicus.eu

client = BackendApplicationClient(client_id=config.sh_client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=config.sh_token_url, client_id=config.sh_client_id, client_secret=config.sh_client_secret, include_client_id=True)

print(token[“access_token”])

Columbia Glacier, Alaska

glacier_bbox = BBox((-147.8, 60.96, -146.5, 61.38), crs=CRS.WGS84)
glacier_size = (700, 466)
time_interval = “2020-07-15”, “2020-07-16”

evalscript_true_color = “”"
//VERSION=3

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

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

request = SentinelHubRequest(
evalscript=evalscript_true_color,
input_data=[
SentinelHubRequest.input_data(
data_collection=DataCollection.SENTINEL2_L2A,
time_interval=time_interval,
)
],
responses=[SentinelHubRequest.output_response(“default”, MimeType.PNG)],
bbox=glacier_bbox,
size=glacier_size,
config=config,
)

image = request.get_data()[0]

And I’m getting this error as below. Token is generated as should as I can print this.

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”}”

Hi, According to your error message your are using the Copernicus Data Space Ecosystem. This is a forum specifically for Sentinel Hub APIs. I would kindly ask you to create a ticket on the Copernicus Data Space Ecosystem community forum, so that your question will benefit other Copernicus Data Space Ecosystem users.

However, make sure you are creating credentials in the right place e.g. the Copernicus Data Space Ecosystem Dashboard

Thanks for your answer William.

So to continue in Sentinel Hub API manner. Is there a fast way to convert my code do this API, or this one is whole different.

When using sentinelhub libraries I thought that I’m using sentinel hub api :frowning:

Best regards
Jacek

You are still using Sentinel Hub APIs but using a different end point. Please refer to this page in the python docs and the Copernicus Data Space Ecosystem documentation

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