首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用Java为Hangouts设置传入的webhook?

用Java为Hangouts设置传入的webhook?
EN

Stack Overflow用户
提问于 2018-06-11 01:50:31
回答 1查看 2.1K关注 0票数 0

我遵循了这里的示例(Incoming webhook with Python),它向Hangout聊天室发送一条简单的消息,并按预期工作

代码语言:javascript
复制
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()

现在,我想用Java实现同样简单的功能,并尝试使用下面的代码

代码语言:javascript
复制
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);
    final HttpResponse response = client.execute(request);

    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);

    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());
}

但这会导致一条错误消息

代码语言:javascript
复制
 {
    "error": {
        "code": 400,
        "message": "Message cannot be empty. Discarding empty create message request in spaces/AAAAUfABqBU.",
        "status": "INVALID_ARGUMENT"
    }
}

我认为我添加json对象的方式有问题。有没有人看到错误所在?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-11 12:53:18

类似于转储,但是在设置请求主体之后移动final HttpResponse response = client.execute(request);行可以解决这个问题。

代码语言:javascript
复制
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());
}

顺序有时确实很重要:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50786490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档