3

I am getting a 401 error when i try to hit the push URL. I am using HTTP BASIC authentication with "Application Key" as username and "Application Master Secret"as password. I am using JAVA HttpsUrlConnection class. I dont know whats wrong with my code.

 `            URL url = new URL("https://go.urbanairship.com/api/push");
          HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setUseCaches(false);
          connection.setRequestProperty("Content-Type","application/json");
          connection.setRequestProperty("Content-Length", Integer.toString(data.length()));

          String authString = "xxxxxxxxxxxxxxxxxx:yyyyyyyyyyyyyyyyy";
          authString = Base64Coder.encodeString(authString);
          connection.setRequestProperty("Authorization","Basic "+ authString);
          OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
          wr.write(data);
          wr.flush();

          int responseCode = connection.getResponseCode();

          //Get the response
          String responseLine = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();`
abhishek
  • 31
  • 2

1 Answers1

2

Your authString should be composed of <application-key>:<application-master-secret>. Also your authstring may not be getting encoded properly. Try using Apache Commons Codec or ostermiller library to encode the authstring

dsr
  • 1,306
  • 1
  • 13
  • 21