0

I am using C# to call OneSignal Create Notifications API. Its working from my home but when i try to send it from my office, it gives a 400 BAD REQUEST. The error that i get is

The error that comes is Please include a case-sensitive header of 
Authorization:   Basic <YOUR-REST-API-KEY-HERE> with a valid REST API key."],
"reference":["https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"]

Also, in office network, when i send it from OneSignal Dashboard it works.

Is it a firewall issue?

Tested with POSTMAN and got the same results. POSTMAN working outside office network and giving 400 Bad Request inside office network.

 using System.IO;
 using System.Net;
  using System.Text;

  var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;

request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";

 request.Headers.Add("authorization", "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjxx");

var serializer = new JavaScriptSerializer();
var obj = new { app_id = "5eb5a37e-b458-11e3-ac11-000c2940e6xx",
           contents = new { en = "English Message" },
           include_player_ids = new string[] {"all"} };



var param = serializer.Serialize(obj);
byte[] byteArray = Encoding.UTF8.GetBytes(param);

string responseContent = null;

 try {
    using (var writer = request.GetRequestStream()) {
       writer.Write(byteArray, 0, byteArray.Length);
    }

   using (var response = request.GetResponse() as HttpWebResponse) {
   using (var reader = new StreamReader(response.GetResponseStream())) {
         responseContent = reader.ReadToEnd();
     }
   }
}
catch (WebException ex) {
   System.Diagnostics.Debug.WriteLine(ex.Message);
   System.Diagnostics.Debug.WriteLine(new    StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
   }
    System.Diagnostics.Debug.WriteLine(responseContent);
Sourav Das
  • 982
  • 1
  • 15
  • 40
  • I sure hope that those aren't your real `app_id` and `authorization` tokens...Additionally, this does appear to me to be a firewall issue or the request is being modified somehow by your work network. – JosephGarrone Mar 15 '17 at 02:52
  • @JosephGarrone Tried with POSTMAN and got the same error. How to check if the request gets modified. The POSTMAN console is same for both – Sourav Das Mar 15 '17 at 19:57
  • It is clear: it is a firewall issue. – user3248578 Feb 12 '18 at 23:27

2 Answers2

1

try following

System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); 
credentialCache.Add(
    new System.Uri("http://www.yoururl.com/"),
    "Basic", 
    new System.Net.NetworkCredential("username", "password")
);

...
...

httpWebRequest.Credentials = credentialCache; 

but i think you should use test RESTFUL tools to check what parameters can return correct value at all first.

Javi Zhu
  • 68
  • 8
0

The issue is solved by adding the onesignal host to our firewall as a trusted site.

Sourav Das
  • 982
  • 1
  • 15
  • 40