Issue with refresh token

Rithvik
Tera Contributor

I used the below code, to generate access token with refresh token, from the below code I am getting access token but for refresh token I am getting "NULL" what is the reason for it? any idea?

 

username and passwords are correct only and also passed correct application registry

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());
1 ACCEPTED SOLUTION

Raghu Ram Y
Kilo Sage

@Rithvik Check from the 3rd party whether it is IP restricted.

View solution in original post

14 REPLIES 14

Community Alums
Not applicable

Hi Rithvik,

You can refer this :

https://community.servicenow.com/community?id=community_question&sys_id=047c9ac3dbb97340feb1a851ca961978

https://community.servicenow.com/community?id=community_question&sys_id=12cf1bfbdbf933000be6a345ca9619c5

 

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

I do that, but can you please verify my above code and help me what is wrong in that?

Rithvik
Tera Contributor

@Saurav @shloke04 @Jaspal Singh 

shloke04
Kilo Patron

Hi @Rithvik 

Sharing below step by step procedure on how you need to do this. Generate Access token and also Refresh token automatically and works for me in my PDI as well:

OAuth 2.0 users access instance resources through external clients by obtaining a token rather than by entering login credentials with each resource request.

find_real_file.png

#Firstly, it is important to understand about Access token & Refresh Token        

Access token: - An instance issues access tokens to clients that have a valid authorization grant. Each access token has a specific scope, lifespan, and other attributes.

By default, an instance issues access tokens with a 30-minute lifespan in the scenario where the instance is the OAuth provider. For third-party tokens, 30 days.

Refresh Token:- A credential that a client uses to obtain new access tokens without requiring additional user authorization. An instance issues a refresh token to a client when it is first authorised to have an access token.

By default, an instance issues refresh tokens with a 100-day lifespan in the scenario where the instance is the OAuth provider. For third-party tokens, 365 days.

find_real_file.png

Steps For Target Instance (Inbound Action)    

 

Step 1: - Navigate to Application Registry-->Create New--> First give the name & after saving a default Client ID and Client Secret are generated along with Refresh & Access Token

find_real_file.png

Step 2: - Navigate to Scripted Rest API-->Create New--> Fill the required mandatory details

find_real_file.png

Step 3: - Create Resource and define HTTP method as POST

 

find_real_file.png

find_real_file.png

Step 4: - After that we need to write script within scripted Rest API as shown below:

find_real_file.png

Step 5:-  Mark as true on Require Authentication 

Create a User with Password by assigning role:- rest_service & web_service admin and share that username & password to Source Instance(Outbound Action)

find_real_file.png

Once this is done, next step would be to cover the Outbound part with steps mentioned below:

STEPS OF Outbound ACTION ON Source Instance

Step 1: - Navigate to System OAUTH--> Application Registry-->Create New

find_real_file.png

Here, we need to fill the Client ID and Client Secret which shared by Target Instance along with Token URL & Grant type--> Resource Owner Password Credentials

After saving the Application Registry, by default it created

find_real_file.png

Step 2: - Navigate to Rest Message --> Create New --> Fill the required field details

find_real_file.png

So here we define the Endpoint which was provided by SOURCE 1 i.e. –

 https://dev****.service-now.com/api/572717/create_incident_pdi

Step 3: - Define Authentication Type 

find_real_file.png

Step 4: - Define HTTP Request

find_real_file.png

Step 5: - Define HTTP Methods named Create Incident PDI with OAuth 2.0 Authentication

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

After saving, a default variable substitution is created called access token

find_real_file.png

Step 6: - Navigate to Script Include--> Create New

find_real_file.png

Step 7: - Navigate to System Definition-->Schedule Job-->Create New and Call the Script Include and Function along with timeline on the bases of which Refresh token generated

find_real_file.png

Step 8: - Create New System Properties and paste the value which we get from Schedule Job-Execute UI Action (Refer to Step 7)

find_real_file.png

Step 9: - Write down a After Business Rule on Table-Incident by defining Insert Operation

find_real_file.png

find_real_file.png

# After completion, test it by creating an Incident Record on Source Instance (Outbound Action) and checked that record is created on Target Instance (Inbound Action) or not.

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke