// Suppose we are preparing an http request in a background thread.
// Setting up the request, e.g. preparing a URLConnection
authgear.refreshAccessTokenIfNeededSync();
} catch (OauthException e) {
String accessToken = authgear.getAccessToken();
if (accessToken == null) {
// The user is not logged in, or the token is expired.
// It is up to the caller to decide how to handle this situation.
// Typically, the request could be aborted
// immediately as the response would be 401 anyways.
HashMap<String, String> headers = new HashMap<>();
headers.put("authorization", "Bearer " + accessToken);
// Submit the request with the headers...