Exchange a grant for an access token
https://api.testingapis.com/api/auth/oauth/tokenBody: application/x-www-form-urlencoded or JSON. Client auth: Authorization: Basic base64(client_id:client_secret), or client_id and client_secret in the body. Clients: apibee-client / apibee-secret (confidential), apibee-public (public, no secret). Users: apibee / password123, jane / jane123.
grant_type values: client_credentials: optional scope; confidential client only; no refresh token. password: username, password, optional scope. authorization_code: code, redirect_uri (if one was sent to /authorize), code_verifier (if PKCE was used). refresh_token: refresh_token. Refresh tokens rotate: each use returns a new one and the old one stops working. Using a spent refresh token again revokes every token from the same grant (reuse detection, RFC 9700). Access tokens expire after 3600 seconds.
Request body
Optional. application/x-www-form-urlencoded. Standard OAuth2 token request (JSON with the same fields also works).
grant_type=client_credentials&client_id=apibee-client&client_secret=apibee-secret&scope=readResponses
- 200
- access_token, token_type, expires_in, refresh_token (not for client_credentials) and scope.
- 400
- invalid_request, invalid_grant, invalid_scope, unauthorized_client or unsupported_grant_type.
- 401
- invalid_client: unknown client or wrong secret.
Example
curl -X POST "https://api.testingapis.com/api/auth/oauth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'grant_type=client_credentials&client_id=apibee-client&client_secret=apibee-secret&scope=read'Cache-Control: no-store
{
"access_token": "oat_b10d78524a730b6c2a480d434ca733a91fc37a81",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "read"
}Code examples
const response = await fetch("https://api.testingapis.com/api/auth/oauth/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: "grant_type=client_credentials&client_id=apibee-client&client_secret=apibee-secret&scope=read",
});
console.log(response.status, await response.text());import requests
response = requests.request(
"POST",
"https://api.testingapis.com/api/auth/oauth/token",
headers={
"Content-Type": "application/x-www-form-urlencoded",
},
data="grant_type=client_credentials&client_id=apibee-client&client_secret=apibee-secret&scope=read".encode("utf-8"),
allow_redirects=False,
)
print(response.status_code, response.text)import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.testingapis.com/api/auth/oauth/token"))
.header("Content-Type", "application/x-www-form-urlencoded")
.method("POST", HttpRequest.BodyPublishers.ofString("grant_type=client_credentials&client_id=apibee-client&client_secret=apibee-secret&scope=read"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}