How to generate a bearer token? (C#)
C# example
This tutorial is written in C# using the IdentityModel.Client library. Download the full C#.NET example project with helper library.
When do you need a bearer token?
You must obtain a bearer token before making any requests to the Platform APIs. The token is used to authenticate and authorize every API call under the OAuth 2.0 Client Credentials flow.
Tokens expire after a set period — your application should detect a 401 Unauthorized response and request a new token.
How to generate a bearer token (C#)
The following steps walk through obtaining an access token from the Platform Identity server using C#.
Libraries used:
IdentityModel.ClientNewtonsoft.JsonSystem.Text
Step 1: Set the variable values
Set identityUrl, clientId, clientSecret, and scope to the appropriate values.
var identityUrl = "https://identity.example.com";
var clientId = "[Your_ClientId]";
var clientSecret = "[Your_ClientSecret]";
var scope = "platform_upload_api";
identityUrl— the URL of the Platform Identity (token) serverclientIdandclientSecret— obtainable from the Platform web appscope— the API scope you want access to:platform_upload_apifor the Upload APIplatform_processing_apifor the Processing API
Step 2: Create an HTTP client
Step 3: Retrieve the OpenID Connect discovery document
var openIdDiscoveryDocument = client.GetDiscoveryDocumentAsync(identityUrl).GetAwaiter().GetResult();
This retrieves the Identity server's configuration, including the token endpoint URL.
Step 4: Build the token request
var request = new ClientCredentialsTokenRequest
{
Address = openIdDiscoveryDocument.TokenEndpoint,
ClientId = clientId,
ClientSecret = clientSecret,
Scope = scope
};
Step 5: Send the request and return the token
var tokenResponse = await client.RequestClientCredentialsTokenAsync(request);
if (tokenResponse != null)
{
return tokenResponse.AccessToken;
}
else
{
Console.WriteLine("Authentication failed");
throw new Exception("Authentication failed");
}
Complete code
using IdentityModel.Client;
using Newtonsoft.Json;
using System.Text;
namespace Platform_API_Demo_Client
{
public static class Identity
{
public static async void Run()
{
var identityUrl = "https://identity.example.com";
var clientId = "[Your_ClientId]";
var clientSecret = "[Your_ClientSecret]";
var accessToken = await Authenticate(identityUrl, clientId, clientSecret, "platform_upload_api");
}
static async Task<string> Authenticate(string identityUrl, string clientId, string clientSecret, string scope)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(identityUrl);
var openIdDiscoveryDocument = client.GetDiscoveryDocumentAsync(identityUrl).GetAwaiter().GetResult();
var request = new ClientCredentialsTokenRequest
{
Address = openIdDiscoveryDocument.TokenEndpoint,
ClientId = clientId,
ClientSecret = clientSecret,
Scope = scope
};
var tokenResponse = await client.RequestClientCredentialsTokenAsync(request);
if (tokenResponse != null)
{
return tokenResponse.AccessToken;
}
else
{
Console.WriteLine("Authentication failed");
throw new Exception("Authentication failed");
}
}
}
}