How to add Authorization header to external REST API call from server script

community
rest

#1

I am trying to call Onesignal push notification REST API by adding custom script to post_process on DF server script.

This is my code
> var url = ‘https://onesignal.com/api/v1/notifications?app_id=APP_ID&included_segments=All’;
> var payload = {
> “contents”: {“en”: “English Message”}
> };
> var payload_data = JSON.stringify(payload);
> var options = {
> ‘headers’: {
> ‘Content-Type’: ‘application/json’,
> ‘Authorization’: ‘Basic AUTHORIZATION_KEY’
> }
> };
> var result2 = platform.api.post(url, payload_data, options);
> var_dump(result2);

But I get following error from Onesignal REST API:

Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST API key

I am considering this has something to do with DF syntax. I couldn’t come across any examples with usage of ‘Authorization’ header in DF API. Sending REST API key is compulsory in Onesignal API.

If anyone in DF community has come across similar issue and has found a resolution or workaround please share your solutions. I would really appreciate any help. Thanks in advance.


#2

It,s easy, try that

function doGet() {
myFunction();
}
function myFunction() {
var url = “https://onesignal.com/api/v1/notifications”;

var D = {
app_id:“OneSignalAppID”,
// include_player_ids : [“1bd1227a-ff50-478c-b097-b2a673c8c817”],
included_segments:[“All”],
headings:{“en” : “FaucetSatoshi”},
contents: {“en” : “PLAYER”},
data: {“type”: “phoneNumber”}
};
var payload = JSON.stringify(D);

var headers = { “Accept”:“application/json”,
“Content-Type”:“application/json”,
“Authorization”:“Basic RESTAPIKey”,

         };

var options = { “method”:“POST”,
“headers”: headers,
“payload” : payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);

}