examples Variables and auth
oauth2.http
Capturing a token by hand compared with the @auth oauth2 directive.
needs your own server
resterm _examples/oauth2.httpRun this from the root of a clone of the Resterm repository.
Editor
OAuth 2.0 directive docs### Simulated OAuth2 Token Exchange
# @name OAuthManualExchange
# @tag oauth2 capture
# @description Use httpbin to echo a client-credentials request and capture the token into globals for reuse.
# @var request oauth.requestId {{$uuid}}
# @capture global-secret oauth.manualToken {{response.json.json.access_token}}
# @capture file oauth.manualRefresh {{response.json.json.refresh_token}}
POST https://httpbin.org/anything/oauth/token
Content-Type: application/json
Accept: application/json
{
"grant_type": "client_credentials",
"client_id": "{{oauth.clientId}}",
"client_secret": "{{oauth.clientSecret}}",
"scope": "{{oauth.scope}}",
"access_token": "manual-{{$uuid}}",
"refresh_token": "refresh-{{$uuid}}",
"token_type": "Bearer",
"expires_in": 3600,
"request_id": "{{oauth.requestId}}"
}
### Call API With Captured Token
# @name OAuthManualReuse
# @tag oauth2 reuse
# @description Pulls the token from the global cache populated by the previous request.
# @capture request oauth.manualAuthHeader {{response.json.headers.Authorization}}
# @assert vars.get("oauth.manualToken") => "token should be available"
# @assert vars.get("oauth.manualToken") in vars.get("oauth.manualAuthHeader") => "Bearer header should include the captured token"
GET {{base.url}}/anything/accounts
Authorization: Bearer {{oauth.manualToken}}
Accept: application/json
### Client Credentials With @auth oauth2
# @name OAuthDirective
# @tag oauth2 directive
# @description Delegates token acquisition to Resterm.
# @auth oauth2 token_url={{oauth.tokenUrl}} client_id={{oauth.clientId}} client_secret={{oauth.clientSecret}} scope="{{oauth.scope}}" audience={{oauth.audience}} cache_key={{oauth.cacheKey}} client_auth=body
# @capture request oauth.directiveAuthHeader {{response.json.headers.Authorization}}
GET {{base.url}}/anything/projects
Accept: application/json
### Reuse Cached Directive Token
# @name OAuthDirectiveReuse
# @tag oauth2 directive reuse
# @description Using the same cache_key allows the cached access token to be sent without another token exchange when it is still valid.
# @auth oauth2 cache_key={{oauth.cacheKey}}
# @capture request oauth.directiveReuseAuth {{response.json.headers.Authorization}}
GET {{base.url}}/anything/projects/{{tenant.id}}
Accept: application/json
### Authorization Code with PKCE
# @name OAuthAuthCodePKCE
# @tag oauth2 authcode
# @description Opens a browser at auth_url and captures the redirect on a loopback callback (PKCE S256 by default).
# @auth oauth2 auth_url={{oauth.authUrl}} token_url={{oauth.tokenUrl}} client_id={{oauth.clientId}} scope="{{oauth.scope}}" grant=authorization_code code_challenge_method=s256
GET {{base.url}}/anything/projects/{{tenant.id}}?flow=auth-code
Accept: application/json