Hey,
if you are using npm or yarn for managing the packages for your project, I suggest using sentinelhub-js which simplifies the usage of Sentinel Hub services.
The updating of the auth token still needs to be done by hand, but it’s easier as it is just a call of a function with client id and secret to request a new token. Checking if token has expired is not part of the library yet.
const authToken = await requestAuthToken(clientId, clientSecret);
setAuthToken(authToken);
It’s also easier to get all the dates, because it enables getting them with or without the auth token. Also, the pagination is used if dates are requested on the Catalog API, so you get all the results with 1 function call.
The library also simplifies getting the images either via WMS or via Processing API.
The most basic setup is presented in the example below (example contains random parameters).
This example doesn’t use Catalog API to get the dates, so Catalog API features are not available here.
import { S2L2ALayer, BBox, MimeTypes, ApiType } from '@sentinel-hub/sentinelhub-js';
const instanceId = ...;
const layerId = ...;
const layer = new S2L2ALayer({
instanceId,
layerId,
maxCloudCoverPercent: 60,
});
const bbox4326 = new BBox(CRS_EPSG4326, 11.9, 42.05, 12.95, 43.09);
const fromTime = new Date(Date.UTC(2020, 1 - 1, 1, 0, 0, 0));
const toTime = new Date(Date.UTC(2020, 1 - 1, 15, 23, 59, 59));
// getting dates
const dates = await layer.findDatesUTC(bbox4326, fromTime, toTime);
// getting images
const getMapParams = {
bbox: bbox4326,
fromTime: fromTime,
toTime: toTime,
width: 512,
height: 512,
format: MimeTypes.JPEG,
}
};
const imageBlob = await layer.getMap(getMapParams, ApiType.WMS);
- To use features of Catalog API and Processing API, the token should be set, but the function calls don’t change, only
setAuthToken(authToken);
should be added before any function, that needs authentication, is called. There is also a function to check if the token was already set, to not call the setAuthToken
unnecessarily.
The library is not yet ready to be used via CDN, so in case you are not using npm or yarn, here are some tips.
-
Requesting a new token is pretty easy to write in javascript and can be packed into a utility function (example in the docs). The solution in sentinelhub-js is a bit different from the example.
async function requestAuthToken(clientId, clientSecret) {
const response = await axios({
method: 'post',
url: 'https://services.sentinel-hub.com/oauth/token',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: `grant_type=client_credentials&client_id=${encodeURIComponent(
clientId,
)}&client_secret=${encodeURIComponent(clientSecret)}`,
});
return response.data.access_token;
}
-
To go through the paginated dates, wrap the request in a while loop and check if the response has a field context.next
(break the loop if the field is not present).
Is it possible to access the Catalog API with an Instance ID?
Unfortunately not when using Catalog API directly, but it can be when using sentinelhub-js (auth token should be still requested and set, but from then on, the layer object can be created only with instance id and layer id).
I am wondering why it is more cumbersome to get a list of dates than to do a FIS request?
The decision to protect some features with authentication (and some also with pagination) was made to prevent abuse and consequent unwanted downtime or slow response since some requests are processed in real time and require more processing power. Getting dates is an example of such request.
Hope this answers as many questions as possible.