Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This getting started guide will show you how to get a JWT token, which then you will use to query the API. Examples are provided in C# .NET using a helper library, using C# .NET HttpClient, a PHP helper library, and in Ruby. The examples are a guideline for the steps, and not necessarily the complete or best practice for the platform.

Examples by platform

...

An example from the token server provider can be found here: https://identityserver4.readthedocs.io/en/release/quickstarts/1_client_credentials.html#creating-the-client


C# using helper library
Anchor
CLIB
CLIB

  1. You want to install the IdentityModel package from the NuGet feed into your client project.
  2. This will enable you to shortcut some of the URL mapping and discovery in the OAuth workflow. The following will do this work for you

    Code Block
    languagec#
    themeConfluence
    firstline1
    titleDiscovery of IDs Metadata
    linenumberstrue
    // discover endpoints from metadata
    var disco = await DiscoveryClient.GetAsync("https://warden.data.uwaterloo.ca");
    if (disco.IsError)
    {
        Console.WriteLine(disco.Error);
        return;
    }
  3. Next you'll pass your credentials to the token server and receive back a JSON response that will include your authorization token.

    Code Block
    languagec#
    themeConfluence
    firstline1
    titleGetting Token from IdentityServer
    linenumberstrue
    var tokenClient = new TokenClient(disco.TokenEndpoint, "your_client_id", "your_client_password");
    var tokenResponse = await tokenClient.RequestClientCredentialsAsync("sourceapi");
    
    if (tokenResponse.IsError)
    {
        Console.WriteLine(tokenResponse.Error);
        return;
    }
    
    Console.WriteLine(tokenResponse.Json);
  4. Now with your valid token you'll be able to make calls against the API to get data.
  5. To send the access token to the API you typically use the HTTP Authorization header. This is done using the SetBearerToken extension method.

    Code Block
    languagec#
    themeConfluence
    firstline1
    titleCalling the API with Token
    linenumberstrue
    var client = new HttpClient();
    client.SetBearerToken(tokenResponse.AccessToken);
    
    var response = await client.GetAsync("https://api.data.uwaterloo.ca/api/v1/terms");
    if (!response.IsSuccessStatusCode)
    {
        Console.WriteLine(response.StatusCode);
    }
    else
    {
        var content = await response.Content.ReadAsStringAsync();
        Console.WriteLine(content);
    }

...