解释:我正在开发一个java应用程序,它通过cpas api(类似于tweepy)发送文本消息。我正在使用curl请求发送消息。来自java的请求似乎已发送,但我收到了401代码。我假设请求的身份验证编码存在问题。代码如下:
URL url = new URL("https://api.zang.io/v2/Accounts/ACe1889084d37de951ef064200aecbe4b2/SMS/Messages");
字符串auth=auth+“:”+令牌;
String encodeedAuth = Base64.getEncoder().withoutPadding().encodeToString(auth.getBytes());
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setRequestProperty("Authorization", "Basic " + encodeedAuth);
String data = "From=+14132698029&To=17817381451&Body=New Test";
byte[] out = data.getBytes(StandardCharsets.UTF_8);
OutputStream stream = http.getOutputStream();
stream.write(out);
System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
http.disconnect();
"""
1条答案
按热度按时间wqlqzqxt1#
http 401响应通常是http端点授权失败。了解身份验证凭据失败原因的最佳方法是捕获传输中的请求/响应对(最好是在接收端),然后将该请求/响应对与有效的请求/响应对进行比较。