- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 01:38 AM
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());
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 04:17 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 01:48 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 02:15 AM
I do that, but can you please verify my above code and help me what is wrong in that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 02:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 03:53 AM
Hi
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.
#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.
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
Step 2: - Navigate to Scripted Rest API-->Create New--> Fill the required mandatory details
Step 3: - Create Resource and define HTTP method as POST
Step 4: - After that we need to write script within scripted Rest API as shown below:
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)
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
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
Step 2: - Navigate to Rest Message --> Create New --> Fill the required field details
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
Step 4: - Define HTTP Request
Step 5: - Define HTTP Methods named Create Incident PDI with OAuth 2.0 Authentication
After saving, a default variable substitution is created called access token
Step 6: - Navigate to Script Include--> Create New
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
Step 8: - Create New System Properties and paste the value which we get from Schedule Job-Execute UI Action (Refer to Step 7)
Step 9: - Write down a After Business Rule on Table-Incident by defining Insert Operation
# 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
Regards,
Shloke