如何解决在没有端口转发的Web服务器的情况下接收Github pubsub事件
我目前已编写以下代码:
public class Connector {
public String auth;
public static void main(String[] args) throws IOException,URISyntaxException {
Connector c = new Connector();
c.setAuthenticationToken( /* token */ );
JsonObject o = new JsonObject();
JsonObject hub = new JsonObject();
hub.addProperty("mode","subscribe");
hub.addProperty("topic","some_event");
hub.addProperty("callback","my_public_IP:some_port");
o.add("hub",hub);
String request = o.toString();
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(c.writeJson("https://api.github.com/hub",REQUEST_METHOD.POST,request))));
}
public void setAuthenticationToken(String token) throws UnsupportedEncodingException {
this.auth = "token " + token;
}
public String writeJson(String strUrl,REQUEST_METHOD method,String data) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(method.name());
con.setRequestProperty("Accept","application/json");
con.setRequestProperty("Content-Type","application/json; utf-8");
con.setRequestProperty("Authorization",this.auth);
con.setDoOutput(true);
if (data != null) {
try (OutputStream os = con.getOutputStream()) {
byte[] input = data.getBytes("utf-8");
os.write(input,input.length);
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuilder response = new StringBuilder();
try {
int statusCode = con.getResponseCode();
InputStream is = null;
if (statusCode >= 200 && statusCode < 400) {
is = con.getInputStream();
} else {
is = con.getErrorStream();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
} catch (IOException e) {
e.printStackTrace();
}
con.disconnect();
return response.toString();
}
打印以下响应:
{
"message": "Invalid hub.callback: \u0027\u0027","documentation_url": "https://docs.github.com/rest/reference/repos#pubsubhubbub"
}
我的目标是打开一个WebSocket,让该WebSocket监听从GitHub发送的事件,而无需使用外部API来转发我网络上的任何端口。
如何指定未打开的ip:port
的回调?
有没有使用WebSocket的替代方法吗?
是否可以创建到https://api.github.com/hub
的传出连接,然后创建指向该传出连接的端口的Webhook,而无需转发该特定端口?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。