Always (invalid_client) Invalid client or Invalid client credentials

When I use the official example (as shown in the Python code below), I consistently encounter the following error: oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client) Invalid client or Invalid client credentials .
Despite changing the OAuth clients twice, the issue persists.

I registered with Sentinelhub, not the Copernicus Data Space Ecosystem, and I am using sentinelhub == 3.10.2 with python == 3.9.5 .

Can anyone help me resolve this problem? Thank you very much.

from sentinelhub import SHConfig

from sentinelhub import (
    CRS,
    BBox,
    DataCollection,
    MimeType,
    SentinelHubRequest,
    bbox_to_dimensions,
)


if __name__ == '__main__':
    
    config = SHConfig()
    config.instance_id = 'USER INSTANCE ID'
    config.sh_client_id = 'sh_client_id'
    config.sh_client_secret = 'sh_client_secret'
    
    config.save()

    if not config.sh_client_id or not config.sh_client_secret:
        print("Warning! To use Process API, please provide the credentials (OAuth client ID and client secret).")
        
    betsiboka_coords_wgs84 = (46.16, -16.15, 46.51, -15.58)
    
    resolution = 60
    betsiboka_bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
    betsiboka_size = bbox_to_dimensions(betsiboka_bbox, resolution=resolution)

    print(f"Image shape at {resolution} m resolution: {betsiboka_size} pixels")
    
    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_true_color = SentinelHubRequest(
        evalscript=evalscript_true_color,
        input_data=[
            SentinelHubRequest.input_data(
                data_collection=DataCollection.SENTINEL2_L1C,
                time_interval=("2020-06-12", "2020-06-13"),
            )
        ],
        responses=[SentinelHubRequest.output_response("default", MimeType.PNG)],
        bbox=betsiboka_bbox,
        size=betsiboka_size,
        config=config,
    )
    
    true_color_imgs = request_true_color.get_data()

I am guessing you are using the wrong end-point.
Check if you have the following in the configuration:
https://services.sentinel-hub.com/api/
and replace it with
https://sh.dataspace.copernicus.eu/api/

Thanks for your reply.

When I use the Copernicus Data Space Ecosystem client, I met the following error: 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 <accessToken>] of your request.", "code": "COMMON_UNAUTHORIZED"}" .

This means you are not really using Copernicus Data Space Ecosystem, but rather sentinel-hub.com
I suggest to search the code for sentinel-hub.com and replace with sh.dataspace.copernicus.eu

You can have a look at how to set-up sh-py for CDSE on readthedocs here.

Thanks, I obviously know to check the documentation.

The problem is solved; I just moved the code to run on an Ubuntu system. Of course, I’m not sure if that was the main reason.

Hello!

I have the same problem. Although I expanded my code with the followings:

# Create a session
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)

# Get token for the session
token = oauth.fetch_token(
    token_url='https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token',
    client_secret=client_secret, include_client_id=True)

# All requests using this session will have an access token automatically added
resp = oauth.get("https://sh.dataspace.copernicus.eu/configuration/v1/wms/instances")

If I print out token, it clearly shows I got one for the session.

I would appreciate if somebody could explain what is the problem. The rest of my code is same as above. I made all credentials, I have my OAuth client and Client secret code too.

Thank you!

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