oAuth bearer token prefix

Programming

I have a requirement to prefix to the Authorization header.. commonly for authenticating we send :
Bearer={token}

However for this use case i need to send this:
xyz Bearer={token}

​

How can i accomplish this using OAuth profile in a REST Message?

Posted Sep 21, 2022, 6:14 PM UTC

11
SitBoySitGoodDog•

I haven't used oAuth yet but...

Here's what I would do:

  1. Create your REST Message and generate any variables for the auth token and another REST Message that you use the auth token.
  2. Generate both of the script in the REST Message by clicking the preview script usage link in the related links list (i think that's what it's called).
  3. Then throw them into a script include. Write 2 functions in the script include...

Something like this:

getToken: function() {
    var r = new sn_ws.RESTMessageV2('yourRestMessage','yourMethod');
    r.setStringParameterNoEscape('yourVariable', this.getToken());
    var response = r.execute();
    var responseBody = response.getBody();

    return responseBody;
}
useAuth: function() {
   var getNewToken = this.getToken();
   var parseToken = JSON.parse(getNewToken).accessToken;

   var token = "xyz Bearer " + parseToken;

    var r = new sn_ws.RESTMessageV2('yourRestMessage','yourMethod');
    r.setStringParameterNoEscape('yourVariable', token);
    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    return httpStatus;
}

So basically you're going to get your auth token first by parsing the response from the getNewToken variable and then store the auth token in a new variable and pass it to your setStringParameterNoEscape.

2