Show / Hide Table of Contents

Authentication API Clients

Bearer tokens are used to authenticate client applications to API services.

Obtaining a bearer token

To access an API you will require a "bearer token" from the relevant identity server.

See - the Identity Server Documentation

This will return a response containing a token (access_token below) that can then be used on subsequent requests.

{
    "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6InQ4alhIQUVOLWpNNkxpVkNyQXBhWmciLCJ0eXAiOiJhdCtqd3QifQ.eyJuYmYiOjE2MTAxMDI5MzYsImV4cCI6MTYxMDEwNjUzNiwiaXNzIjoiaHR0cHM6Ly9hcy1kZXYtZGFlcmEtaWRlbnRpdHkuYXp1cmV3ZWJzaXRlcy5uZXQiLCJhdWQiOiJzZXJ2aWNlLmFkbWluLmFwaSIsImNsaWVudF9pZCI6IkRFQ09MLkNsaWVudC5BcHAiLCJjbGllbnRfRG9tYWluSWQiOiIxMDI3MyIsInNjb3BlIjpbInNlcnZpY2UuYWRtaW4uYXBpIiwic2VydmljZS5hZG1pbi5hcGkuZGVjb2xtaSIsInNlcnZpY2UuYWRtaW4uYXBpLnNlY3VyZW1lc3NhZ2luZyJdfQ.18dNIkQc9MwGJ8nIKpq8ccK9fTY358pC1BCGuZr2hzBbxB5iFArR3DbCBHJ_1IKLHjLpvGr9NqSSGqKjGEZm8XQ_WPZBWJDVnYPFymUxLEGCiPJd-tWh6BGc0DdOhV0gPH6kkeiEa-UHOoOZIUndOq1d3zcqkriUs9_7izyZoNUpS99BhKjL3wBuBpt2DRpAeoqbj6asnliFr_3aYMQ7OwdUDkIGgwp6ZBhF3FA7MHq3nL6K14GL3XlVX4Gh2IGHbcLRru1NtpNck5XQAzVj__GMM6pUiVQzLP5Eas2AVTlodsizU9CAlq66p8tUCsG-g9W6NPL5-kgoXAS1rNxXIw",
    "expires_in": 3600,
    "token_type": "Bearer",
    "scope": "service.admin.api service.admin.api.decolmi service.admin.api.securemessaging"
}

In requests to the service then set the "Authorization" header to be "Bearer >access_token>", where <access_token> is the token from the above request.

C# snippets

Obtaining a bearer token

using (HttpClient client = new HttpClient())
{
    TokenResponse tokenResult = null;

    // requires IdentityModel nuget package
    // Install-Package IdentityModel
    // further detail on the token endpoint available https://identitymodel.readthedocs.io/en/latest/client/token.html
    tokenResult = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
    {
        Address = securityAuthorityUrl,
        ClientId = clientid,
        ClientSecret = clientsecret,
        Scope = scopes
    });

    bearerToken = tokenResult.AccessToken;
}

Using a bearer token

using (HttpClient client = new HttpClient())
{
    HttpRequestMessage request = new HttpRequestMessage();
    request.Method = HttpMethod.Get;
    request.RequestUri = new Uri("http://www.exampleapi.com/api/getdata");

    // Add Bearer Authorization header
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);

    HttpResponseMessage result = null;
    result = await client.SendAsync(request);
}
In This Article
Back to top Powered by The Stiona Digital Transformation Toolit