Service tokens
Mint an appcenter:service token with client_credentials, cache it properly and understand the two refusals.
Your backend holds a confidential_service client. It mints short-lived tokens with the client_credentials grant and calls the Application API. There is no user involved: a service token carries no sub and cannot be used for anything a person sees.
Mint one
curl -X POST https://apps-api.versified.com.gh/oidc/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=ledgerly-backend" \
-d "client_secret=$LEDGERLY_CLIENT_SECRET" \
-d "scope=appcenter:service" \
-d "resource=urn:versified:api:appcenter"
# {
# "access_token": "eyJ...",
# "token_type": "Bearer",
# "expires_in": 900,
# "scope": "appcenter:service"
# }Practical notes
- Cache the token until 60 seconds before expiry. One request per token is wasteful and needlessly loads the provider.
- The token is RS256. When you are the client, treat it as opaque — App Center verifies it, not you.
- A user token is refused.
/app/*is service-only: a browser session token gets403 service_token_required. Never proxy a person's token into the Application API. - The client must be bound to an application. Register it under an application; an unbound client gets
403 app_not_bound.
let cached: { token: string; expiresAt: number } | null = null;
export async function serviceToken(): Promise<string> {
if (cached && cached.expiresAt - 60_000 > Date.now()) return cached.token;
const response = await fetch(`${ISSUER}/token`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
grant_type: "client_credentials",
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
scope: "appcenter:service",
resource: "urn:versified:api:appcenter",
}),
});
if (!response.ok) {
throw new Error(`token request failed: ${response.status}`);
}
const body = await response.json();
cached = {
token: body.access_token,
expiresAt: Date.now() + body.expires_in * 1000,
};
return cached.token;
}import time
import requests
_cached: tuple[str, float] | None = None
def service_token() -> str:
global _cached
if _cached and _cached[1] - 60 > time.time():
return _cached[0]
response = requests.post(
f"{ISSUER}/token",
data={
"grant_type": "client_credentials",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"scope": "appcenter:service",
"resource": "urn:versified:api:appcenter",
},
timeout=10,
)
if response.status_code != 200:
raise RuntimeError(f"token request failed: {response.status_code}")
body = response.json()
_cached = (body["access_token"], time.time() + body["expires_in"])
return _cached[0]import static java.nio.charset.StandardCharsets.UTF_8;
import jakarta.json.Json;
import java.io.StringReader;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
private record Cached(String token, long expiresAt) {}
private Cached cached;
synchronized String serviceToken() throws Exception {
if (cached != null
&& cached.expiresAt() - 60_000 > System.currentTimeMillis()) {
return cached.token();
}
String body = "grant_type=client_credentials" +
"&client_id=" + URLEncoder.encode(CLIENT_ID, UTF_8) +
"&client_secret=" + URLEncoder.encode(CLIENT_SECRET, UTF_8) +
"&scope=" + URLEncoder.encode("appcenter:service", UTF_8) +
"&resource=" + URLEncoder.encode("urn:versified:api:appcenter", UTF_8);
HttpRequest request = HttpRequest.newBuilder(URI.create(ISSUER + "/token"))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new IllegalStateException(
"token request failed: " + response.statusCode());
}
var json = Json.createReader(
new StringReader(response.body())).readObject();
cached = new Cached(
json.getString("access_token"),
System.currentTimeMillis() + json.getInt("expires_in") * 1000L);
return cached.token();
}Prove the credential at boot
Call GET /appcenter/v3/app/me once on startup. It answers with the application your client is bound to, which turns a misconfigured environment variable into an obvious log line instead of a mystery 403 an hour later.
Service tokens in the other direction
App Center also mints service tokens — for retill:service and onekitchen:service — when it calls *your* capability endpoints. The difference is who holds the secret, not the shape of the token. Verify it the same way: signature, issuer, then audience.
Something here out of date or unclear?
The platform team answers integration questions directly.
