使用 httpsurlconnection 类的 Urbanairship 出现 401 错误

发布于 2024-10-18 00:11:28 字数 1197 浏览 1 评论 0原文

当我尝试点击推送 URL 时收到 401 错误。我使用 HTTP BASIC 身份验证,“应用程序密钥”作为用户名,“应用程序主密钥”作为密码。我正在使用 JAVA HttpsUrlConnection 类。我不知道我的代码有什么问题。

 `            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();`

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();`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2024-10-25 00:11:29

您的 authString 应由 : 组成。此外,您的身份验证字符串可能无法正确编码。尝试使用 Apache Commons Codecostermiller 库对授权字符串进行编码

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文