【发布时间】:2016-09-07 12:45:02
【问题描述】:
我指的是来自http://api.pushetta.com/pushetta-docs/的java示例
我确实通过了正确的通道和令牌(API 密钥),但我得到了
Response Code : 403
Response Message : FORBIDDEN
你能建议吗?谢谢。
这是我正在使用的代码:
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 to 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 you wanna print it
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");
}
}
【问题讨论】:
-
显示你的代码,全部。
-
@KrzysztofCichocki - 添加
-
仅供参考 - 我尝试使用用户名/密码向 api.pushetta.com/api/auth 发送 POST - 从 Java 执行时返回 400 Bad Request 和 401 Unathorized {"detail": "CSRF Failed: CSRF token missing或不正确。"} 通过浏览器 REST 客户端执行时
-
另外,仅供参考 - 使用 python 代码很好
-
您确定此频道名称正确吗?它是用小写字母还是大写字母写的,甚至可能很重要。
标签: java push-notification http-status-code-403