Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Dynamically created RESTMessageV2 and oauth authentication

ENo
Tera Contributor

Hello

 

We have some code that dynamically creates and adds the necessary(?) parameters to a RESTMessageV2 objects. This includes an oauth2 authentication profile. The problem is that when executes the REST request no authentication data is added to the request. We can see that the header contains Authorization=null and that no oauth authorization request are performed. If we change the request to a statically defined REST message (sys_rest_message) with the same oauth profile, the authorization works as expected. Here is a simple code snippet we have used to try to figure out what the problem might be:

var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod("get");
restMessage.setEndpoint(<endpoint URL>);
restMessage.setAuthenticationProfile('oauth2', '59d3d984b597c71032949b08bd99ee00');
restMessage.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
var response = restMessage.execute();
responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();
status = response.getStatusCode();
gs.info("status=" + status + ", responseBody=" + responseBody);

This example fails as described above (ie no oauth authorization is performed).

0 REPLIES 0