none
Slack authentication by c# RRS feed

  • Question

  • 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. For other accounts response includes only x cookie. 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;
            }



    Friday, June 7, 2019 2:58 PM

All replies

  • Hi KorSah,

    Thank you for posting here.

    Since this thread is related to Slack API, I suggest that you could ask in the following link.

    https://api.slack.com/faq

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Note:This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 10, 2019 2:11 AM