c# - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() -
i want sent push notification via parse.com , have webservice that
[webmethod] public bool pushnotification(string pushmessage) { bool ispushmessagesend = false;
string poststring = ""; string urlpath = "https://api.parse.com/1/push"; //var httpwebrequest = (httpwebrequest)webrequest.create(urlpath); system.net.webrequest request = system.net.httpwebrequest.create(urlpath); request.usedefaultcredentials = true; request.preauthenticate = true; request.credentials = credentialcache.defaultcredentials; poststring = "{ \"channels\": [ \"trials\" ], " + "\"data\" : {\"alert\":\"" + pushmessage + "\"}" + "}"; request.contenttype = "application/json"; request.contentlength = poststring.length; request.headers.add("app id", "my parse app id"); request.headers.add("rest api key", "my rest api key"); request.method = "post"; streamwriter requestwriter = new streamwriter(request.getrequeststream()); requestwriter.write(poststring); requestwriter.close(); //var httpresponse = (httpwebresponse)request.getresponse(); system.net.webresponse response = request.getresponse(); using (var streamreader = new streamreader(response.getresponsestream())) { var responsetext = streamreader.readtoend(); jobject jobjres = jobject.parse(responsetext); if (convert.tostring(jobjres).indexof("true") != -1) { ispushmessagesend = true; } } return ispushmessagesend; }
i had error this
system.net.webexception: remote server returned error: (401) unauthorized. @ system.net.httpwebrequest.getresponse() @ webservice.pushnotification(string pushmessage) in c:\users\hurkan seyhan\desktop\website3\app_code\webservice.cs:line 488
how can solve problem?
according documentation (https://parse.com/docs/push_guide#sending-channels/rest) think mistake in headers
instead of
request.headers.add("[your app id]", "my parse app id"); request.headers.add("[your rest api key]", "my rest api key");
you should write following:
request.headers.add("x-parse-application-id", "[your app id]"); request.headers.add("x-parse-rest-api-key", "[your rest api key]");
Comments
Post a Comment