Slack authentication by c#

  • Thread starter Thread starter KorSah
  • Start date Start date
K

KorSah

Guest
I created a slack app which has to read messages from the channel and another app must post messages to another channel from another workspace. But the problem is that I can't install the first app because administrative privileges required. I have an account in the workspace from where is needed to read messages. I want to do authentication with my credentials and then get the token from response, and so I can read messages by that token.

In the workspase I have two accounts. I am able to login with one of the account but it does not work for others. What is going wrong?

public static async Task<string> Get(Uri url)
{
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
var response = string.Empty;

using (var client = new HttpClient(handler))
{
HttpResponseMessage result = await client.GetAsync(url);
responseCookies = cookies.GetCookies(url).Cast<Cookie>();
if (result.IsSuccessStatusCode)
{
response = await result.Content.ReadAsStringAsync();
}
}
return response;
}


public static async Task<string> Post(Uri url, string postData, CookieContainer cookies)
{
var response = string.Empty;

using (var handler = new HttpClientHandler() { CookieContainer = cookies })
using (var client = new HttpClient(handler) { BaseAddress = url })
{
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.DefaultRequestHeaders.Add("Cache-Control", "max-age=0");
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
//client.DefaultRequestHeaders.ConnectionClose = false;
client.DefaultRequestHeaders.Add("Origin", "null");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
//client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
client.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage result = await client.PostAsync(url,
new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded"));
if (result.IsSuccessStatusCode)
{
response = await result.Content.ReadAsStringAsync();
}
}
return response;
}

Continue reading...
 
Back
Top