【问题标题】:get slack channel history in .net web api在 .net web api 中获取松弛频道历史记录
【发布时间】:2016-02-16 08:49:06
【问题描述】:

我在 .Net Web API 中获取了频道历史记录。 slack 引用 https://api.slack.com/methods/channels.history 它指出我们需要发布请求。

如果有人可以帮助我编写代码,请。

我已经实现的部分代码:

   #region create json payload to send to slack

        GetLatestMessage payload = new GetLatestMessage()
        {
            channel = "###",//value.channel_name,
            token = "############################"//added the token i have generated
            // user_name = value.user_name,
            //text = value.text
        };

      #endregion


        string payloadJson = JsonConvert.SerializeObject(payload);

        using (WebClient client = new WebClient())
            {

            NameValueCollection data = new NameValueCollection();
            data["payload"] = payloadJson;
            var response = client.UploadValues("https://slack.com/api/channels.history", "POST", data);


            //The response text is usually "ok"
           string responseText = _encoding.GetString(response);
           LogFileWriter("response=" + responseText);
            return Request.CreateResponse(HttpStatusCode.OK);
            }

【问题讨论】:

  • Stackoverflow 不是免费的编码服务,人们可以在其中完成您的工作。表现出一些努力。你试过什么?什么代码不起作用?您在实现代码时遇到了哪些问题?
  • @RononDex 我已经尝试过我的代码。现在添加我的代码。看看你能不能帮忙。
  • 好的,现在您的代码的哪一部分不起作用?
  • 我得到的响应是: response={"ok":false,"error":"not_authed"} 这意味着'没有提供身份验证令牌。'但是当我直接在 url (slack.com/api/channels.history?token=###&channel=####) 中传递相同的令牌时,我得到了预期结果的正确 json 响应。
  • 现在好多了,如果您使用该信息更新您的问题,我相信有人可以帮助您

标签: c# slack-api


【解决方案1】:

我发现了我面临的问题。我试图将 post json 数据发送到 Slack url。但是,Slack Web API 不接受 JSON 数据。现在,当我使用标准 HTTP 表单属性发布数据时,它接受并返回正确的响应。

新代码:

 var response = client.UploadValues("https://slack.com/api/channels.history", "POST", new NameValueCollection() {
{"token","###################"},
{"channel","######"}});

            //The response text is usually "ok"
           string responseText = _encoding.GetString(response);
           LogFileWriter("response=" + responseText);
            return Request.CreateResponse(HttpStatusCode.OK);
            }

【讨论】:

  • 干得好。很高兴你能自己解决:)
  • 非常有帮助,通过 client.UploadString 的正常 POST 与 POST 正文中的 GET 参数的相同字符串不起作用。这个有效。我想我在根据This question 编码数据时犯了一些错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 2014-08-16
  • 2018-05-10
  • 2011-12-31
相关资源
最近更新 更多