使用Java为Hangouts聊天API设置传入webhook?
我在这里遵循了这个示例(),它向一个聊天室发送一条简单的消息,并按预期工作使用Java为Hangouts聊天API设置传入webhook?,java,hangouts-chat,Java,Hangouts Chat,我在这里遵循了这个示例(),它向一个聊天室发送一条简单的消息,并按预期工作 from httplib2 import Http from json import dumps def main(): url = 'https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>' bot_message = { 'text' : 'Hello from Python
from httplib2 import Http
from json import dumps
def main():
url = 'https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>'
bot_message = {
'text' : 'Hello from Python script!'}
message_headers = { 'Content-Type': 'application/json; charset=UTF-8'}
http_obj = Http()
response = http_obj.request(
uri=url,
method='POST',
headers=message_headers,
body=dumps(bot_message),
)
print(response)
if __name__ == '__main__':
main()
我假设我添加json对象的方式有问题。有人看到错误了吗?类似于转储,但移动行
final HttpResponse response=client.execute(request)代码>设置请求正文后解决问题
private void sendPost() throws IOException {
String url = "https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>";
final HttpClient client = new DefaultHttpClient();
final HttpPost request = new HttpPost(url);
// FROM HERE
request.addHeader("Content-Type", "application/json; charset=UTF-8");
final StringEntity params = new StringEntity("{\"text\":\"Hello from Java!\"}", ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(params);
// TO HERE
final HttpResponse response = client.execute(request);
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
private void sendPost()引发IOException{
字符串url=”https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=";
最终HttpClient=新的DefaultHttpClient();
最终HttpPost请求=新HttpPost(url);
//从这里
addHeader(“内容类型”,“应用程序/json;字符集=UTF-8”);
最终的StringEntity参数=新的StringEntity(“{\”text\“:\”来自Java的你好!\“}”,ContentType.APPLICATION\u FORM\u URLENCODED);
请求.setEntity(参数);
//到这里
最终HttpResponse响应=client.execute(请求);
final BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuffer结果=新的StringBuffer();
弦线;
而((line=rd.readLine())!=null){
结果。追加(行);
}
System.out.println(result.toString());
}
顺序有时确实很重要:)某种转储,但移动行final HttpResponse response=client.execute(请求)代码>设置请求正文后解决问题
private void sendPost() throws IOException {
String url = "https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>";
final HttpClient client = new DefaultHttpClient();
final HttpPost request = new HttpPost(url);
// FROM HERE
request.addHeader("Content-Type", "application/json; charset=UTF-8");
final StringEntity params = new StringEntity("{\"text\":\"Hello from Java!\"}", ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(params);
// TO HERE
final HttpResponse response = client.execute(request);
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
private void sendPost()引发IOException{
字符串url=”https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=";
最终HttpClient=新的DefaultHttpClient();
最终HttpPost请求=新HttpPost(url);
//从这里
addHeader(“内容类型”,“应用程序/json;字符集=UTF-8”);
最终的StringEntity参数=新的StringEntity(“{\”text\“:\”来自Java的你好!\“}”,ContentType.APPLICATION\u FORM\u URLENCODED);
请求.setEntity(参数);
//到这里
最终HttpResponse响应=client.execute(请求);
final BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuffer结果=新的StringBuffer();
弦线;
而((line=rd.readLine())!=null){
结果。追加(行);
}
System.out.println(result.toString());
}
秩序有时很重要:)
private void sendPost() throws IOException {
String url = "https://chat.googleapis.com/v1/spaces/AAAAUfABqBU/messages?key=<WEBHOCK-KEY>";
final HttpClient client = new DefaultHttpClient();
final HttpPost request = new HttpPost(url);
// FROM HERE
request.addHeader("Content-Type", "application/json; charset=UTF-8");
final StringEntity params = new StringEntity("{\"text\":\"Hello from Java!\"}", ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(params);
// TO HERE
final HttpResponse response = client.execute(request);
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}