External Two squares, one overlapping the bottom one. Top square has an arrow pointing away, as if leading you away

Using offline access and refresh tokens

Purpose

A guided walk-through for using the offline_access scope and refresh tokens to call Zywave APIs without requiring a user to be present and re-authenticate each time.

Background

This page applies only to apps using User-based OAuth 2.0. If your app uses System-based OAuth 2.0 instead, offline_access and refresh tokens aren't relevant. There's no user session to keep alive.

By default, the access token returned from the Authorization Code grant type is short-lived, and the user would need to log in again once it expires. Requesting the offline_access scope changes this: it's applicable to any Zywave API that authenticates using the Authorization Code grant type, and causes the token response to also include a refresh token. That refresh token can then be exchanged for new access tokens going forward, without the user needing to log in again.

Walk-through

Initial setup

When offline_access is requested, the user is always prompted to consent, even if they've consented before. There's no option to remember that consent for future logins.

Making API calls with a refresh token

Zywave strongly recommends using a library for your preferred language or framework for working with OAuth 2.0 patterns. The below are purely for informational purposes.

Example:

      curl --request POST --url https://auth.zywave.com/connect/token --header 'Content-Type: application x-www-form-urlencoded' --data "client_id=[YOUR_CLIENT_ID]" --data "client_secret=[YOUR_CLIENT_SECRET]" --data "grant_type=refresh_token" --data "refresh_token=[YOUR_REFRESH_TOKEN]"
    
  
using System.Net.Http;
using System.Net.Http.Headers;

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://auth.zywave.com/connect/token");
request.Content = new StringContent("client_id=[YOUR_CLIENT_ID]&client_secret=[YOUR_CLIENT_SECRET]&grant_type=refresh_token&refresh_token=[YOUR_REFRESH_TOKEN]");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application x-www-form-urlencoded");

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://auth.zywave.com/connect/token"))
    .POST(BodyPublishers.ofString("client_id=[YOUR_CLIENT_ID]&client_secret=[YOUR_CLIENT_SECRET]&grant_type=refresh_token&refresh_token=[YOUR_REFRESH_TOKEN]"))
    .setHeader("Content-Type", "application x-www-form-urlencoded")
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
fetch('https://auth.zywave.com/connect/token', {
  method: 'POST',
  headers: {
    'Content-Type': 'application x-www-form-urlencoded'
  },
  body: 'client_id=[YOUR_CLIENT_ID]&client_secret=[YOUR_CLIENT_SECRET]&grant_type=refresh_token&refresh_token=[YOUR_REFRESH_TOKEN]'
});
import requests

headers = {
    'Content-Type': 'application x-www-form-urlencoded',
}

data = 'client_id=[YOUR_CLIENT_ID]&client_secret=[YOUR_CLIENT_SECRET]&grant_type=refresh_token&refresh_token=[YOUR_REFRESH_TOKEN]'

response = requests.post('https://auth.zywave.com/connect/token', headers=headers, data=data)