Sending Push notification using Pushetta in Java results in 403 Forbidden -


i'm referring java example http://api.pushetta.com/pushetta-docs/

i pass correct channel , token (api key), i'm getting

response code : 403 response message : forbidden 

can please advise? thanks.

this code i'm using:

public class pushnotification {  // settings public static string channel = "recognition"; public static string token = "e9897f1ce470f302bdfc8c8167ff489fb0c0fc19";  public static void sendnotification(string message) {      string url = "http://api.pushetta.com/api/pushes/" + channel + "/";      try {         url obj = new url(url);          httpurlconnection con = (httpurlconnection) obj.openconnection();          // set reuqest header         con.setrequestmethod("post");         con.setrequestproperty("host", "api.pushetta.com");         con.setrequestproperty("user-agent", "chrome");         con.setrequestproperty("authorization", token);         con.setrequestproperty("accept-language", "en-us,en;q=0.5");         con.setrequestproperty("content-type", "application/json");          // api parameters         string urlparameters = "{ \"body\" : \"" + message + "\", \"message_type\" : \"text/plain\"  }";          // send post request         con.setdooutput(true);         dataoutputstream wr = new dataoutputstream(con.getoutputstream());         wr.writebytes(urlparameters);         wr.flush();         wr.close();          int responsecode = con.getresponsecode();         string responsemessage = con.getresponsemessage();         system.out.println("\nsending 'post' request url : " + url);          // response information         system.out.println("post parameters : " + urlparameters);         system.out.println("response code : " + responsecode);         system.out.println("response message : " + responsemessage);          // read response         bufferedreader in = new bufferedreader(new inputstreamreader(con.getinputstream()));         stringbuffer response = new stringbuffer();         string inputline;         while ((inputline = in.readline()) != null) {             response.append(inputline);         }         in.close();          // complete html page response if wanna print         string res = response.tostring();          // extract result         int aux = res.indexof("success");         int auxx = res.indexof(",", aux);         system.out.println(res.substring(aux, aux + 7) + " : " + res.substring(aux + 15, auxx));      } catch (malformedurlexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (protocolexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }   } 

}

public class test {    public static void main(string[] args) {     pushnotification.sendnotification("recognized");   } } 

it seems i've found it, in examples, authorization header takes form:

"authorization: token {api key}"

so you've missed token prefix before token. change line:

 con.setrequestproperty("authorization", token); 

to

 con.setrequestproperty("authorization", "token " + token); 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -