Watson Campaign Automation OAuth tokens have a lifetime of 4 hours, but tokens can be refreshed after 3 hours. If you request before the start of the fourth hour, you receive the same access token that you obtained earlier. If you request after the start of the third hour, a new access token is granted. The user account is going to have two access tokens available to them to use (the first token expires in 1 hour and the second token expires in 4 hours. Request a new token after 3 hours to avoid the unexpected expiration of a single access token.

Java™ code example that is used to create an OAuth access token for API usage.

Example Code

	package com.sp.example;
	import net.sf.json.JSONObject;
	import net.sf.json.JSONSerializer;
	import org.apache.commons.httpclient.HttpClient;
	import org.apache.commons.httpclient.methods.PostMethod;
	import java.io.IOException;
	import java.io.InputStream;
	import java.util.Scanner;
	* Created with IntelliJ IDEA.
	* User: exampleuser
	* Date: 6/21/13
	* Time: 3:20 PM
	* To change this template use File | Settings | File Templates.
	public class AccessTokenRetriever {
	public static final String PARAM_CLIENT_ID = 'client_id';
	public static final String PARAM_CLIENT_SECRET = 'client_secret';
	public static final String PARAM_REFRESH_TOKEN = 'refresh_token';
	public static final String PARAM_GRANT_TYPE = 'grant_type';
	public static final String GRANT_TYPE = 'refresh_token';
	private String url;
	private HttpClient httpClient;
	private String responseText;
	public AccessTokenRetriever(String url) {
	this(url, new HttpClient());
	AccessTokenRetriever(String url, HttpClient httpClient) {
	this.url = url;
	this.httpClient = httpClient;
	public String retrieveToken(String clientId, String clientSecret, String refereshToken) {
	PostMethod post = createPost(clientId, clientSecret, refereshToken);
	try {
	responseText = getResponseText(post);
	return getTokenFromResponse();
	} catch (Exception e) {
	throw new RuntimeException(e);
	private PostMethod createPost(String clientId, String clientSecret, String refereshToken) {
	PostMethod post = new PostMethod(url);
	post.setParameter(PARAM_CLIENT_ID, clientId);
	post.setParameter(PARAM_CLIENT_SECRET, clientSecret);
	post.setParameter(PARAM_REFRESH_TOKEN, refereshToken);
	post.setParameter(PARAM_GRANT_TYPE, GRANT_TYPE);
	return post;
	private String getResponseText(PostMethod post) throws IOException {
	InputStream is = post.getResponseBodyAsStream();
	Scanner scanner = new Scanner(is).useDelimiter('A');
	return scanner.hasNext() ? scanner.next() : '';
	private String getTokenFromResponse() throws IOException {
	JSONObject json = (JSONObject) JSONSerializer.toJSON(responseText);
	return json.getString('access_token');
	public static void main(String[] args) {
	String url = 'https://engage1.silverpop.com/oauth/token';
	String clientId = 'XMLAPIDEVQA';
	String clientSecret = 'NOT_FOR_PRODUCTION_USE';
	String refreshToken = 'ac1bfab0-f310-4161-9f6b-53cabac451a8';
	AccessTokenRetriever tokenRetriever = new AccessTokenRetriever(url);
	String accessToken = tokenRetriever.retrieveToken(clientId, clientSecret, refreshToken);
	System.out.println('access_token: ' + accessToken);
	* In future requests
	* httpHeaders.set('Authorization', 'Bearer ' + accessTokenFor(oauthClientIdentification.value()));


1 comment on"OAuth Token Management in Java"

  1. Igor Soares October 24, 2018

    I can not compile this code. I have already imported the JAR files but it continues with an error. Is this code working?

Join The Discussion

Your email address will not be published. Required fields are marked *