GET Token - JAVA

The Token service is a compulsory RESTful API for integrated users to retrieve a token which will be submitted as part of the request header for authentication purposes on every API request call. A valid token must be provided within each API call. Tokens are only valid for 20 minutes. The Token API will be added by default to all APPs created.

REVEAL integration users are provided with a REST username and password. The integration credentials will not be the same as the user’s email address and password used to access the integration manager portal. Every REST integration credential is associated with one REVEAL account.

To retrieve a successful token, the integration username and password needs to be converted to BASE64, as shown in the sample below.

Main Method

public class Program{
  public static void main(String[] args){
    // Set local variables
    String username = "username";
    String password = "password";
    String getTokenUri = "";

    // Pass variables to create a new TokenGenerator object    
    TokenGenerator token = new TokenGenerator(username, password, getTokenUri);
    // Display auth string    	

Token Generator Class

import java.util.Base64;
import java.util.Date;
import java.util.Calendar;

  public class TokenGenerator{
    private String _authString; 
    private Date _expiredDateTime;
    private String _username;
    private String _password;
    private String _getTokenUri;
    // Public property that returns the token string. The logic contained
    // evaluates if the token is expired when it is accessed. If expired, 
    // a new token is automatically populated, and new token string returned	
    public String GetAuthString(){
      Date now = new Date();
        this._authString = GetToken();
      return this._authString; 

    // Constructor that takes username, password, and enpoint for the Token API	
    public TokenGenerator(String username, String password, String getTokenUri){
      _username = username;
      _password = password;
      _getTokenUri = getTokenUri;
      _authString = GetToken();
    private String GetToken(){
      String authString = "";
        // Get authentication, Base64 encoded, string	  
        String basicAuth = BuildBasicAuth(_username, _password);
        URL url = new URL(_getTokenUri);                                       // Construct a url with the earlier supplied GetToken URI
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();   // Open a new Https connection for transmission
        conn.setRequestMethod("GET");                                          // Set request type to GET
        conn.setRequestProperty("Authorization", basicAuth);                   // Pass in auth string into Authorization header
        conn.setRequestProperty("Accept", "text/plain");                       // Denote type of response expected
        conn.setDoOutput(false);                                               // Do not send request body

        // Execute request, read response. If we do not receive a 200 OK, throw exception		
        if (conn.getResponseCode() != HttpsURLConnection.HTTP_OK) {
          throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());

        // Reads text from a character-input stream
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        this.SetExpiredDateTime();             // Set expiration date of token (expires after 20 minutes)
        authString = br.readLine();            // Read the buffer into the authString variable, not including any line-termination characters
        conn.disconnect();                     // Close HttpsURLConnection
      } catch (MalformedURLException e) {      // Required Exception
      } catch (IOException e) {                // Required Exception
      return authString;

    // Supply username and password to build a Base64 encoded authentication string
    private static String BuildBasicAuth(String username, String password) {
      String userCredentials = username + ":" + password;                                   // Prepare user credentials for encoding 
      return "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());     // Base64 encode the user credentials and append to "Basic " and return 
    // Set the newly acquired token's expiration date and time
    private void SetExpiredDateTime(){
      Calendar now = Calendar.getInstance();      // Get current date time 
      now.add(Calendar.MINUTE, 20);               // Add twenty minutes (token is invalid after this time frame) 
      this._expiredDateTime = now.getTime();      // set expired date time to a date representing the time value plus twenty minutes.

To download the source code .zip for this example, click HERE.


All sample code is provided by Fleetmatics, A Verizon Company, for illustrative purposes only. These examples have not been thoroughly tested under all conditions. Fleetmatics, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All programs contained herein are provided to you “AS IS” without any warranties of any kind.