OAuth 2.0 Bearer Access Token Authorization

 HERE Maps API for JavaScript version 3.2

The HERE Maps API for JavaScript supports OAuth 2.0 bearer access tokens for authorizing requests to HERE services. Bearer tokens provide an alternative to API key authorization, letting you use short-lived credentials instead of a static key.

📘

Note

In browser-based cross-origin requests, using the Authorization header can trigger a CORS preflight request before the actual request is sent. This adds an extra network round trip and might affect performance, particularly on high-latency connections or when making many requests.

Prerequisites

Configuration

You can configure custom headers on H.service.Platform. These headers are included in requests to all platform services.

You can also set custom headers for individual service instances.

Configure a bearer token for all platform services

To configure the HERE Maps API for JavaScript to use an OAuth 2.0 bearer access token, include the token in the Authorization header when initializing the platform instance, as shown in the following example:

const platform = new H.service.Platform({
  headers: {
    Authorization: 'Bearer <YOUR_ACCESS_TOKEN>'
  }
});
📘

Note

Providing an API key to the platform instance becomes optional when using OAuth 2.0 bearer access tokens for authorization. Similarly, providing an OAuth 2.0 bearer access token becomes optional when you provide an API key.

Configure a bearer token for individual service instances

To set custom headers for individual service instances, include the headers in the service instance configuration:

const omvService = platform.getOMVService({
  headers: {
    Authorization: 'Bearer <YOUR_ACCESS_TOKEN>'
  }
});

Alternatively, you can define the headers in a services configuration object and pass it to the platform instance. The following example sets the Authorization header for the HERE Vector Tile API v2 service and initializes the platform with the services configuration:

const servicesConfig = {
  // HERE Vector Tile API v2
  [H.service.omv.Service.CONFIG_KEY]: {
    headers: {
      Authorization: 'Bearer <YOUR_ACCESS_TOKEN>'
    }
  }
};
// Initialize the platform with the services configuration.
// In this example, requests to HERE Vector Tile API v2 will be authorized using the provided token while
// requests to other services will use the API key provided in the platform configuration.
const platform = new H.service.Platform({
  apikey: '<API_KEY>',
  servicesConfig
});

See H.service.Platform and H.service.Options API reference pages for more details.

Related content


Did this page help you?