- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 08:02 AM
Hi.
I'm building a REST Message application to get data from a third-party API. However I'm stuck at the authentication setting.
To get the data I need, I need to use a bearer token, but this token only lasts for one hour. So I need to get the token (using POST) and, after it, make a GET request.
I can get the token, but I have no idea how I pass it as an authentication parameter to the POST request.
Has anyone faced this problem?
Thanks for the help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 04:17 PM
Hi Lucas,
You can create script include which will be using Rest Message to generate token - this will be function 1 then you can create second function and call there Rest Message to get data and pass token to this function from first function. In that scenario second function will trigger token generation each time when it will run. Example below
getToken: function() {
try {
var rm = new sn_ws.RESTMessageV2('xxx', "Token");
var response = rm.execute();
var responsebody = response.getBody();
var httpStatus = response.getStatusCode();
var a = JSON.parse(responsebody);
var token = a.access_token;
return token;
} catch (ex) {
var message = ex.getMessage();
gs.error("xxx:Token:Error: " + message);
}
},
getlist: function() {
try {
var date = new GlideDate();
date.addMonthsUTC(-1);
var rm = new sn_ws.RESTMessageV2('xxx', "GET list");
rm.setStringParameter('date', date);
rm.setStringParameter('token', this.getToken());
var response = rm.execute();
var responsebody = response.getBody();
var httpStatus = response.getStatusCode();
var a = JSON.parse(responsebody);
return a;
} catch (ex) {
var message = ex.getMessage();
gs.error("xxx:getlist:Error: " + message);
}
},
In Rest Message you need to add token like below (name of the parameter may be different then authorization but it will be described in documentation from 3rd party)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 04:17 PM
Hi Lucas,
You can create script include which will be using Rest Message to generate token - this will be function 1 then you can create second function and call there Rest Message to get data and pass token to this function from first function. In that scenario second function will trigger token generation each time when it will run. Example below
getToken: function() {
try {
var rm = new sn_ws.RESTMessageV2('xxx', "Token");
var response = rm.execute();
var responsebody = response.getBody();
var httpStatus = response.getStatusCode();
var a = JSON.parse(responsebody);
var token = a.access_token;
return token;
} catch (ex) {
var message = ex.getMessage();
gs.error("xxx:Token:Error: " + message);
}
},
getlist: function() {
try {
var date = new GlideDate();
date.addMonthsUTC(-1);
var rm = new sn_ws.RESTMessageV2('xxx', "GET list");
rm.setStringParameter('date', date);
rm.setStringParameter('token', this.getToken());
var response = rm.execute();
var responsebody = response.getBody();
var httpStatus = response.getStatusCode();
var a = JSON.parse(responsebody);
return a;
} catch (ex) {
var message = ex.getMessage();
gs.error("xxx:getlist:Error: " + message);
}
},
In Rest Message you need to add token like below (name of the parameter may be different then authorization but it will be described in documentation from 3rd party)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 10:10 AM
Thanks for your help. It was exactly what I was looking for. Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 03:55 AM
I am using same code for generating access token but getting error :-
Error:-: { "code" : "390146", "message" : "Bearer token is missing in the HTTP request authorization header." }
kindly help, if You have any idea regarding this.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 04:07 AM