GlideOAuthClient - getToken(String oauthEntityName, String requestor)

Retrieves the token for the client. You can use the token to check the expiration date and perform a token renewal.

Table 1. Parameters
Name Type Description
OAuthEntityName String The OAuth entity.
requestor String The request.
Table 2. Returns
Type Description
ScopedGlideOAuthToken The token for the client.

This example code retrieves a token from the instance database:

token = oAuthClient.getToken(testAppProvider, someone@someemail.com);

This example code retrieves a token using the password grant type.

var tokenRequest = new  sn_auth.GlideOAuthClientRequest();
tokenRequest.setGrantType("password");
tokenRequest.setUserName("itil");
tokenRequest.setPassword("itil");
tokenRequest.setRequestor("someone@someemail.com");
tokenRequest.setScope(null);
 
var oAuthClient = new  sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("testAppProvider", tokenRequest);
gs.info("Error:" + tokenResponse.getErrorMessage());
 
var token = tokenResponse.getToken(); 
dumpToken(token);
//call function below
This example code retrieves a token from the instance database:
token = oAuthClient.getToken(testAppProvider, someone@someemail.com);
dumpToken(token);
//call function below

This example code retrieves a token using the refresh token grant type.

var tokenRequest = new sn_auth.GlideOAuthClientRequest();
tokenRequest.setGrantType("refresh_token");
tokenRequest.setRefreshToken(token.getRefreshToken());
tokenRequest.setRequestor("someone@someemail.com");
 
var oAuthClient = new sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("testAppProvider", tokenRequest);
gs.info("Error:" + tokenResponse.getErrorMessage());
 
var token = tokenResponse.getToken(); 
dumpToken(token);
//call function below

This function displays the access token, refresh token, and expiration date and time for the access token.

function dumpToken(token) {
  if(token) {
       gs.info("AccessToken:" + token.getAccessToken());
       gs.info("AccessTokenExpiresIn:" + token.getExpiresIn());
       gs.info("RefreshToken:" + token.getRefreshToken());
  }
}