Get client access token

Prerequisites

Use a library

WARNING

We do not recommend implementing this flow manually. There are good OAuth 2.0 libraries for all common languages.

You are expected to reuse the obtained token until it expires. It can be kept in memory, and you are not required to keep a shared cache for your servers.

This is handled automatically for some libraries; Go's clientcredentialsopen in new window TokenSource function supplies an up-to date token with automatic refreshing, which is also the case for our Java library (github.com/working-group-two/wgtwo-authopen in new window).

Example

Get client credentials for subscription.read and subscription.write

#!/usr/bin/env bash
export CLIENT_ID="my client ID"
export CLIENT_SECRET="my client secret"

curl -u "${CLIENT_ID}:${CLIENT_SECRET}" \
  --request POST \
  --url 'https://id.wgtwo.com/oauth2/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type="client_credentials" \
  --data scope="subscription.read subscription.write"
1
2
3
4
5
6
7
8
9
10
Install dependencies

Maven

<dependency>
  <groupId>com.wgtwo.api</groupId>
  <artifactId>auth</artifactId>
  <version>0.0.5</version>
</dependency>
import com.wgtwo.auth.WgtwoAuth

fun main() {
    val clientId = System.getenv("CLIENT_ID")
    val clientSecret = System.getenv("CLIENT_SECRET")
    val wgtwoAuth = WgtwoAuth.builder(clientId, clientSecret).build()

    val scope = "subscription.read subscription.write"
    val token = wgtwoAuth.clientCredentials.fetchToken(scope)

    println("Got access token ${token.accessToken} which expires at ${token.expiry}")
}
1
2
3
4
5
6
7
8
9
10
11
12

Example usage

File not found
File not found

Example usage

File not found

Resources

Reference

OAuth 2.0 Reference