[英]Why do I get "not_authed"-error from Slack when sending a post-request from c#?
I am trying to serialize an object into Json and then send it to Slack.我正在尝试将对象序列化为 Json,然后将其发送到 Slack。 I have done this successfully without serializing but instead using "Dictionary" and "FormUrlEncodedContent" and then send it.我在没有序列化的情况下成功完成了这项工作,而是使用“Dictionary”和“FormUrlEncodedContent”然后发送它。 But now, for the purpose of making things easier and more agile, I just wanted to create one JSon-class which I could serialize and then use for every request I want to send.但是现在,为了让事情变得更简单和更敏捷,我只想创建一个 JSon 类,我可以将它序列化然后用于我想要发送的每个请求。
Here is my code:这是我的代码:
public class JsonObject
{
private string _token = "xoxp-MyToken";
[JsonProperty("token")]
public string token { get { return _token; } }
[JsonProperty("channel")]
public string channel { get; set; }
[JsonProperty("as_user")]
public bool as_user = true;
[JsonProperty("username")]
public string username { get;set; }
[JsonProperty("text")]
public string text { get; set; }
}
public class BpsHttpClient
{
private readonly HttpClient _httpClient = new HttpClient { };
public Uri UriMethod { get; set; }
public BpsHttpClient(string webhookUrl)
{
UriMethod = new Uri(webhookUrl);
}
public async Task<HttpResponseMessage> UploadFileAsync(StringContent requestContent)
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, UriMethod);
request.Content = requestContent;
var response = await _httpClient.SendAsync(request);
return response;
}
}
class MainArea
{
public static void Main( string[] args)
{
try
{
Task.WaitAll(SendMessage());
}
catch(Exception ass)
{
Console.WriteLine(ass);
Console.ReadKey();
}
}
private static async Task SendMessage()
{
var client = new BpsHttpClient("https://slack.com/api/chat.postMessage");
JsonObject JO = new JsonObject();
JO.channel = "DCW21NBHD";
JO.text = "This is so much fun :D !";
var Json = JsonConvert.SerializeObject(JO, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var StringJson = new StringContent(Json, Encoding.UTF8, "application/json");
var DeSon = JsonConvert.DeserializeObject(Json);
Console.WriteLine(DeSon); //this is for me to see if my JsonObject looks correct - it does ;)
Console.ReadKey();
var Response = await client.UploadFileAsync(StringJson);
string AnswerContent = await Response.Content.ReadAsStringAsync();
Console.WriteLine(AnswerContent);
Console.ReadKey();
}
} }
When I run the code I allways get the answer:当我运行代码时,我总是得到答案:
Output:输出:
{"ok":false,"error":"not_authed"}
although I think my JsonObject looks right - it has the token in there... Anybody have an idea why?虽然我认为我的 JsonObject 看起来不错 - 它在那里有令牌......有人知道为什么吗?
So, i figured it out - I SHALL NOT put my token in the JsonObject I want to send.所以,我想通了 - 我不会把我的令牌放在我想发送的 JsonObject 中。
The solution in this case (using httpclient
) is that one has to add a header for authorization to the client, like so:在这种情况下的解决方案(使用httpclient
)是必须添加一个标头以授权给客户端,如下所示:
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "lé token");
and then it works.然后它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.