Documentation Center

C# code sample for connecting to Tridion Docs OpenAPI with OpenID Connect and client credentials flow

This topic contains example C# code that uses the OpenID Connect client credentials flow to connect to Tridion Docs OpenAPI. This code sample is provided "as is," without any guarantees, and is only intended as a basic sample to get you started. For example, it does not perform any error handling.

using System.Net.Http.Headers;
using Newtonsoft.Json;
using OpenApiConsoleExample.OpenApiClients;

// Get an access token from Access Management.
const string tridionDocsUrl = "https://tridiondocs.example.com";   // Replace with the base URL of your Tridion Docs Content Manager server
const string serviceAccountClientId = "TridionDocs_CM_API_Client"; // Replace with your own client ID
const string serviceAccountClientSecret = "yFJSCq-8=c4jbpT7";      // Replace with your own client secret

// Call the GetAccessToken() method to obtain a token.
AccessToken accessToken = await GetAccessToken(tridionDocsUrl, serviceAccountClientId, serviceAccountClientSecret);

// Set up the authenticated communication to Tridion Docs.
using HttpClient httpClient= new()
{
    BaseAddress = new Uri(tridionDocsUrl + "/ISHWS/Api/")
};
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(accessToken.TokenType, accessToken.Token);
TridionDocsClient tridionDocsClient = new(httpClient);

// Verify the connection by making a call to request the Tridion Docs version.
Console.WriteLine($"Tridion Docs version: {await tridionDocsClient.GetApplicationVersionAsync()}");

// Verify the connection by asking Tridion Docs to identify me.
User user = await tridionDocsClient.GetCurrentUserAsync(SelectedProperties.TitleAndDescription, FieldGroup.None, null, false);
Console.WriteLine($"I am {user.Title} with identifier {user.Id}");

return;

// The GetAccessToken() method gets an access token from Access Management using the client credentials flow
async Task<AccessToken> GetAccessToken(string authenticationServerUrl, string clientId, string clientSecret)
{
    using HttpClient client = new()
    {
        BaseAddress = new Uri(authenticationServerUrl)            
    };

    List<KeyValuePair<string, string>> body =
    [
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
        new KeyValuePair<string, string>("client_id", clientId),
        new KeyValuePair<string, string>("client_secret", clientSecret)
    ];

    using HttpResponseMessage responseMessage = await client.PostAsync("/ISHAM/connect/token", new FormUrlEncodedContent(body));

    string token = await responseMessage.Content.ReadAsStringAsync();

    AccessToken serviceAccountToken = JsonConvert.DeserializeObject<AccessToken>(token)!;

    return serviceAccountToken;
}

// The AccessToken reference type
internal record AccessToken(
    [property:JsonProperty("access_token")]
    string Token,

    [property:JsonProperty("expires_in")]
    int ExpiresIn,

    [property:JsonProperty("token_type")]
    string TokenType
);