在 Java 中使用 Pushetta 发送推送通知导致 403 Forbidden

Sending Push notification using Pushetta in Java results in 403 Forbidden

我指的是 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");
  }
}

看来我已经找到了,在所有示例中,授权 header 采用以下形式:

"Authorization: Token {API KEY}"

所以您错过了令牌前的 Token 前缀。 更改此行:

 con.setRequestProperty("Authorization", token);

 con.setRequestProperty("Authorization", "Token " + token);