Error 401 with urbancarehip using the httpsurlconnection class

I get error 401 when I try to push the url. I use HTTP BASIC authentication with “Application Key” as the username and “Application Wizard Password” as the password. I am using the JAVA class HttpsUrlConnection. I do not know what is 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();`
+3
source share
1 answer

Your authString should consist of <application-key>:<application-master-secret>. In addition, your authentication may not be correctly encoded. Try using Apache Commons Codec or ostermiller to encode authstring

+2
source

Source: https://habr.com/ru/post/1792755/


All Articles