GlideOAuthClient - Scoped, Global

Use these methods for requesting and revoking OAuth refresh and access tokens.

This class is available in scoped and global scripts. In a scoped script, use the sn_auth namespace identifier.

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());
  }
}

GlideOAuthClient - requestToken(String clientName, String jsonString)

Retrieves the token for the client, with the request parameters encoded in JSON format.

Table 3. Parameters
Name Type Description
clientName String The client name.
jsonString String The JSON string for the client.
Table 4. Returns
Type Description
GlideOAuthClientResponse The token for the client.

This example shows a resource owner password grant type request, with request parameters encoded in JSON format.

var oAuthClient = new GlideOAuthClient();
var params ={grant_type:"password", username:"itil", password:'itil'};
var json =new JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('TestClient', text);
var token = tokenResponse.getToken();

gs.log("AccessToken:"+ token.getAccessToken());
gs.log("AccessTokenExpiresIn:"+ token.getExpiresIn());
gs.log(" RefreshToken:"+ token.getRefreshToken());

GlideOAuthClient - requestTokenByRequest(String clientName, GlideOAuthClientRequest request)

Retrieves the token for the client, with the client name and the request set into a GlideOAuthClientResponse object.

Table 5. Parameters
Name Type Description
clientName String The client name.
request GlideOAuthClientRequest The request.
Table 6. Returns
Type Description
GlideOAuthClientResponse The token for the client.

GlideOAuthClient - revokeToken(String clientName, String accessToken, String refreshToken, GlideOAuthClientRequest request)

Revokes the access or refresh token for the client, with the request and optional header parameters set into a GlideOAuthClientRequest object.

Table 7. Parameters
Name Type Description
clientName String The client name.
accessToken String The access token.
refreshToken String The refresh token.
request GlideOAuthClientRequest The request.
Table 8. Returns
Type Description
GlideOAuthClientResponse The token for the client.