Exchange or refresh an access token
Issue an enriched access token (and rotated refresh token) via OAuth 2.0 Token Exchange (RFC 8693) or refresh-token grant; also mounted at /api/v1/auth/token-exchange for backward compatibility
post
https://localhost:8080/api/v1/auth/tokenbash
curl --request POST \
--url 'https://localhost:8080/api/v1/auth/token' \
--header 'Content-Type: application/json' \
--data '{"grant_type":"urn:ietf:params:oauth:grant-type:token-exchange","refresh_token":"string","subject_token":"string","subject_token_type":"urn:ietf:params:oauth:token-type:access_token"}'javascript
const response = await fetch("https://localhost:8080/api/v1/auth/token", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
})
});
const data = await response.json();typescript
interface TokenResponse {
access_token?: string;
expires_in?: number;
issued_token_type?: string;
refresh_expires_in?: number;
refresh_token?: string;
scope?: string;
token_type?: string;
}
const response = await fetch("https://localhost:8080/api/v1/auth/token", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
})
});
if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
const data: TokenResponse = await response.json();python
import requests
response = requests.post(
"https://localhost:8080/api/v1/auth/token",
headers={
"Content-Type": "application/json",
},
json={
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token",
},
)
response.raise_for_status()
print(response.json())go
package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
body := strings.NewReader(`{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}`)
req, err := http.NewRequest(http.MethodPost, "https://localhost:8080/api/v1/auth/token", body)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
fmt.Println(res.Status, string(data))
}java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Example {
public static void main(String[] args) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://localhost:8080/api/v1/auth/token"))
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("""
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}
"""))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}csharp
using System.Text;
using var client = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:8080/api/v1/auth/token");
request.Content = new StringContent("""
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}
""", Encoding.UTF8, "application/json");
using var response = await client.SendAsync(request);
Console.WriteLine($"{(int)response.StatusCode} {await response.Content.ReadAsStringAsync()}");php
<?php
$ch = curl_init('https://localhost:8080/api/v1/auth/token');
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => <<<'JSON'
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}
JSON,
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
echo $status . " " . $response . PHP_EOL;ruby
require "net/http"
uri = URI("https://localhost:8080/api/v1/auth/token")
request = Net::HTTP::Post.new(uri)
request["Content-Type"] = "application/json"
request.body = <<~'JSON'
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}
JSON
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request(request)
end
puts "#{response.code} #{response.body}"rust
// Cargo.toml dependencies: reqwest (features "json"), serde_json, tokio (features "full")
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let response = reqwest::Client::new()
.post("https://localhost:8080/api/v1/auth/token")
.json(&json!({
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}))
.send()
.await?;
println!("{} {}", response.status(), response.text().await?);
Ok(())
}Request bodyrequired
Token request
application/json
grant_typestring (enum)requiredOne of
urn:ietf:params:oauth:grant-type:token-exchangerefresh_tokenrefresh_tokenstringRequired for the refresh_token grant
subject_tokenstringRequired for the token-exchange grant
subject_token_typestring (enum)token-exchange grant; access_token only
One of
urn:ietf:params:oauth:token-type:access_token
Examplejson
{
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"refresh_token": "string",
"subject_token": "string",
"subject_token_type": "urn:ietf:params:oauth:token-type:access_token"
}Responses
200Token response
application/json
access_tokenstringexpires_inintegerAccess token lifetime in seconds
issued_token_typestringrefresh_expires_inintegerRefresh token lifetime in seconds
refresh_tokenstringscopestringtoken_typestring
Examplejson
{
"access_token": "string",
"expires_in": 0,
"issued_token_type": "string",
"refresh_expires_in": 0,
"refresh_token": "string",
"scope": "string",
"token_type": "string"
}400Invalid request
application/json
errorstringerror_descriptionstring
Examplejson
{
"error": "Unauthorized",
"error_description": "Invalid token"
}401Invalid or expired token
application/json
errorstringerror_descriptionstring
Examplejson
{
"error": "Unauthorized",
"error_description": "Invalid token"
}500Internal server error
application/json
errorstringerror_descriptionstring
Examplejson
{
"error": "Unauthorized",
"error_description": "Invalid token"
}