# OAuth2 Authentication

> The legacy client-credentials flow, still supported for existing server-to-server integrations.

HTML version: [OAuth2 Authentication](https://support.gladstonesoftware.cloud/guides/oauth2-authentication)

To integrate with the **Gladstone API**, you must first complete authentication. This ensures that your application can securely connect and interact with our services.

Before you can authenticate, please **contact our support team**. We will provide you with the credentials and configuration details required to access our API.

You will receive the following:

* **Client ID** – A unique identifier for your application.
* **Client Secret** – A secure key used to authenticate your app.
  * ⚠️ This secret can only be generated **once** and includes an **expiry date**.
* **Well Known configuration** – Contains all the essential authentication details, including the **Access Token URL**.

Once you have these credentials, you can begin authenticating and making requests to the Gladstone API.

# How to authenticate

There are a lot of ways to authenticate, but we recommend and support token authentication. You can find the token authentication URL in the *Well-Known Configuration* that is provided to you.

## Token Generation

You can generate an access token using either an API testing tool like **Postman** or can generate it Programmatically. Following are the instructions listed for generated it via Postman and **C#**.

### 🔹 Postman (Recommended)

1. Go to Postman and navigate to Auth section.
2. Choose **OAuth 2.0** as the Auth Type.
3. In section **Configure New Token**, configure the settings as per the following:
   * **Token Name:** Descriptive Token Name (e.g., Gladstone API Token)
   * **Grant Type:** `Client Credentials`
   * **Access Token URL:** Use the URL from your *Well-Known Configuration* that you have been provided with by our support team.
   * **Client ID:** Your unique Client ID
   * **Client Secret:** Your unique Client Secret
   * **Scope:** `cloud api`
   * **Client Authentication:** Set to `Send as Basic Auth header`
4. Once the above details are entered, click **Get New Access Token**.

![Configurations for generating Access Token via Postman](../content/assets/guides/5ed0326c1e30f31f257d2e0a7b04c1f4e74823988fa14679e4edee42f69fea9e-image.png)

### 💻 C\#

Use the following Example C# code snippet to authenticate to our API and generate an access token:

```csharp
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "<environment_baseURL>/identity/connect/token");
var collection = new List<KeyValuePair<string, string>>();
collection.Add(new("grant_type", "client_credentials"));
collection.Add(new("client_id", "<your_client_ID>"));
collection.Add(new("client_secret", "<your_client_secret>"));
collection.Add(new("scope", "cloud api"));
var content = new FormUrlEncodedContent(collection);
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
```

## Connecting to Gladstone API

The generated token can now be used in the **Authorization header** of all API calls:

```bash
Authorization: Bearer <your_access_token>
```

### 🧾 Request Structure

You can use the following `curl` command or Postman to send a request to the Gladstone API:

```text
curl --location --request GET 'https://api.gladstone.com/v1/example-endpoint' \
--header 'Authorization: Bearer eyJhbGciOi...'
```

That should be it! Just make sure you are able to authenticate to the Gladstone API with the generated Auth Token.

For troubleshooting or renewal of credentials, contact **Gladstone Support**.
