logo

NJP

Integration between 2 ServiceNow instances | OAUTH 2.0 | use-case | GlideOAuthClient API | Chapter 6

Import · Aug 22, 2021 · video

hi everyone welcome back this is the sixth video in the series of integrations and in this video again we'll talk about the integration between two service now instances instance a and instance b uh in the past we have discussed about it using basic auth as the authentication type but in this video we'll deep dive into the os 2.0 configuration so the instance which is open in front of you right now is my instance a and this is my instance b so let's get started first of all in my instance b i'm going to set up an endpoint so let's click on new call i'll click on create an auth o api endpoint for external clients client id is automatically generated so i am going to give it this name demo whats endpoint and let's reduce it to 600 and let's reduce this to let's say 1800 and i'm going to save my record so my client secret is automatically generated now i'm going to go to my instance b and i'll do the corresponding activity sorry this is my instance a not b the previous one was instance b and now i need to again configure an odd server with the client id and client secret which are present as part of the indices b so here i am going to select this option connect to a third-party oauth provider so let's copy the client id from here and i will again copy the client secret as well now let's use resource on a password credentials this one i have modified to 1800 so let's do that here as well because it will be in sync with this field and now coming to the next part i need to specify a token url which should be for my instance b so let me copy the url and i'm going to paste it over here let's save this so this part is done and a default authentic profile is also created when i save this record now i am going to navigate to list messages and i will create one for this okay corresponding name and i'm going to select authentication type as was 2.0 the profile is this one demo what server default profile which was just created let me save this record so it says that what access of research tokens are not available and click on get token since i am using the resource owner profile let's click on get watch token first let's see how it goes so it is asking me for username and password since i am using the resource owner password credentials as the type in my application registry let me show that to you again over here so i need a valid account in the instance b which can be used over here so let's create a user profile click new okay then the user profile is created and now i'm going to try again so that i am able to get an access token under the first token i am back in my instance a list message and this is the risk message which i created couple of seconds back if i click on get what token now i can specify the profile which exists in the other instance let's click on get oauth token and see if this works fine so this worked and token should be available now since i have set the time to half an hour the refresh token will expire in next half an hour let's reload the page once and we can also take a look at the generated tokens over here go to manage tokens and you can see over here that an access token is generated and a fish token is also available same can be used as long as these are valid to carry out any further operations for example creating an incident in the other instance now this was the one time activity but as i said my first token is only valid for 30 minutes and that was that is just done for this particular demo but in usual cases as well my refresh token would be valid for let's say a couple of months but what after that so so it's not to come back to this list message and do this manual activity we need to come up with something so that once a particular refresh token is expired the next one will be automatically generated and we don't need to do anything uh manually so to accomplish that what can be done is i can create a scheduled job which makes sure that whenever a fish token is available it is stored in one of the system property and i can fetch it from that system property to generate access tokens in the near future let's see how we can do that let's navigate to schedule jobs and i'm going to select a manuscript of your choosing and let's say my refresh token is valid for three months so either i can run this particular script let's say periodically and i can select after every 90 days and i can give the name as first token and let's so i'm going to call one of my script includes over here here it is and my function name would be generate refresh token all right let's save the schedule job and copy the function name now to generate the refresh token via a script i am going to use auth client api so let's see how we can make use of the server side api which is available out of the box so first of all the name of my api is glide okay it doesn't show up this way that's fine so i will have to write it manually i think a over here should be in caps so i have declared my glide or client api now using this api i need to request a token so i am going to call it this way or click dot there is a function named request token which not only gives us the refresh token but access token as well as the expiry time of the access token so the first parameter here is the name of my oauth profile which i have created in the instance so let's go back and copy the same it's this one and the second parameter would be the couple of other parameters which are mandatory for us to communicate with the other instance like the grant type of the request the user profile which we are using to generate that particular token so let me quickly add those things first one is going to be again type okay second one is username so the name of the user which i am using is over here demo underscore what there's a better way to pass these parameters by storing them in a system property not including in the code itself but i'm simply writing it in the server side script itself just to quickly wrap up the things also is same now i need to convert it into json and code it and send it over and i can simply pass this over here okay so this is all about generating a token via this particular script so in this tokens object i will be having access token office token and the expiry time of my access token so you have to change the name to response now here i can and using the tokens object now i can simply get my first token [Music] i can either store it in a variable either i can simply pass it to a system property let's directly pass it this is the name of a function which i am trying to recall i think it is gs.property if it doesn't work we'll come back to it later so here i need to pass the name of the property and then i need to pass the this token over here so i have not passed the name of the system property yet because it is not yet created so let's do it right away and save it let's pass the property name and save us include as well so as and when that schedule job will run it will uh this piece of code and generate the research token for us and save it into that particular system property so if i see this now it is empty and let's take a scheduled job and try to execute it to check whether it is actually working fine or not where are you yes execute now and so this is my refresh token which is generated and is being set into the system property so this code will work we don't need to worry about that now the next part is i want the ability to be able to create an incident in the instance b using the configuration that i have already done so whatever we have done let's summarize that first first of all we have created an end point in the instance b which should be accessible from instance a we have created second part as we created the configuration in the instance a to point to the end point to generate a token from the instance b third part we created a rest message and to get started with the things we clicked on get oauth token and entered the credentials of the user profile that we created in the instance b that has to be a starting point there is no other way until and unless we are planning to go ahead with only the glide client api and the fourth part is we made sure that even if the refresh token expires after a couple of months in our case i considered three months because that is by default which service now considers so after three months my refresh token will be automatically generated so that things don't get big and there is no manual intervention required so these four points are done now i will be trying to create an incident in the instance b using the access token and yes that's it let's get started so let me go back to the rest message which i created so this is my list message and let's create a new http method over here and i am going to name it create incident it should be of type post and here the end point i i'm going to give the end point of table api for now so what should be the ui let's copy okay so so my first header would be content type i'm going to send json data for now i'm going to select authentication as no and my second header would be authorization and and and here i need to pass my access token so how can i pass that let's create a web so that i don't have to hard code it and whatever access token is applicable at that particular moment i'm gonna get it and i'm going to set this particular variable now let's add some content short description yes next comes my description part okay then let's save this and how to generate variables i have hardcoded the short description description for now but these also ideally should come from the incident itself so as of now the access token value is empty and and okay i think this works fine for now and i'm going to go back to the business rules which will trigger this particular test message [Music] okay wait a minute let me first copy the script which is available so that i don't have to make an extra effort [Music] okay so let's click on new instance b table should be incident advanced for now i'm going to use after insert because i am not planning to store the response so let it take whatever time it wants to take and it will be triggered immediately the after the record is inserted into the database and now coming to the script part i need to pass on my access token over here so the one which we requested in the beginning by clicking on get pause token was valid only for let's say couple of minutes so in the future if something is needed we can simply generate it by writing some scripts and it will be generated using the same refresh token which we already have as part of one of the system properties so let's see how we can do that so i'm going to proceed in the same way this is my script include and i'm going to get access token i'm creating a function over here now i need to pass the system property which is going to give me my refresh token so ideally whenever we are creating such functions in a script include the ideology is that it should be loosely coupled so that it can be used by multiple different integrations which are already there in your instance so that's what i'm doing here [Music] okay okay texas token i'm wondering if this is a reserved function or word which is used in the backend by service now so let's change the name to get demo access token so this is where my first token is passed by me already in the business rule and now i'm going to see how i can request an access token using my research token so there is another api which is available out of the box and that is also a server side api the name of the api is glide oauth client request i hope i have used everything oh sorry this needs to be menu since i'm creating an object of glide or client request api and then i'm going to set the client type which in this case should be research token since i am not requesting the first token and access token from the scratch i am only requesting an access token i already have the first token with me so it is going to be the first token and then i need to set my refresh token as well which i am already getting from my business rule passed into this function as a parameter so what next if you want you can also pass scope i have not defined any scope yet so i will pass it as null or empty now i'm going to again use my oauth client api to request access token um so this is the same function which we have already used okay control space doesn't work here here again i need to pass the name of my profile which i created and that is demo or server and the second parameter would be my client request this one so this would give me a response in return and i can use this response to get tokens and i can simply return my access token from here all right so this part is done and i have my access token from here which i am further going to use in my business tool so so that i can pass it to my rest message and accordingly i should be able to create an incident so let's save this and i am back in my business tool i am getting an access token over here which i need to pass in my code so this looks fine and how do i know that something is created let me add an info message but i won't see this because it might take some time but let's see how this is going to work let's create an incident now create new it doesn't matter what shop description and description we're passing since i've already hard coded these and let's save this record okay so something is created i can see the response on my screen and this is the incident number let's go check it out it in the instance b if it is actually created or not okay yes it seems to be created this is this is the short description which i have given as a hard-coded one in my instance a for some reason description has not populated but that could be due to several other reasons which needs to be looked into for some reason or the other we can check it later but this is how an auth integration works first of all you need to generate that refresh token you by clicking on get watch token which is your starting point and for the future cases you can simply use the glide or client apis and generate your research token and using that research token again you can generate access token it depends on you how you want to do it whether you want to rely on the scripting part using your server-side apis or whether you want to completely rely on the rest message and do it that particular way so that's pretty much it thank you for watching and don't forget to subscribe

View original source

https://www.youtube.com/watch?v=X8k4OeB-zU8