如何通过守护程序在团队频道中发送消息

时间:2020-04-10 23:04:49

标签: azure-active-directory microsoft-graph-api microsoft-teams

我想在没有用户通过HTTP调用从外部应用程序向团队频道发送消息的情况下。为此,我已经按照here

的说明创建了应用

现在,我已经按照下面的建议编写了JAVA代码来发送消息:enter link description here 但是由于不支持Type Application权限,因此似乎无法通过HTTP调用发送消息。我得到401

     URL url = new URL("https://graph.microsoft.com/beta/teams/<tanentid>/channels/<channelid>/messages");

JSONObject obj = new JSONObject();
obj.put("content", "Hello world");
JSONObject mainobj = new JSONObject();
mainobj.put("body", obj);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
conn.setRequestProperty("Accept","application/json");
conn.addRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
String postBody = mainobj.toString();
conn.getOutputStream().write(postBody.getBytes());

int httpResponseCode = conn.getResponseCode();
if(httpResponseCode == 200) {
    BufferedReader in = null;
    StringBuilder response;
    try{
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String inputLine;
        response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    } finally {
        in.close();
    }
    return response.toString();
} else {
    return String.format("Connection returned HTTP code: %s with message: %s",
            httpResponseCode, conn.getResponseMessage());
}

此HTTP调用工作正常:https://graph.microsoft.com/beta/teams/{id}/channels

1)是否有可能? 2)如果不是,那么Bot是否可以基于外部应用程序通过HTTP调用输入的消息来发送消息?

0 个答案:

没有答案