.NET Authentication and Authroization

Hi All,

I am having trouble with my code and was wondering if anyone could help point me in the right direction. Currently I am following the Authrorization Code Flow and have steps 1 and 2 working:

1.Send the user you’d like to authenticate to this URL:
https://api.twitch.tv/kraken/oauth2/authorize
?response_type=code
&client_id=[your client ID]
&redirect_uri=[your registered redirect URI]
&scope=[space separated list of scopes]
&state=[your provided unique token]

We support the state OAuth2 parameter, which is strongly recommended for avoid cross-site scripting attacks. If included, it is appended to the list of query parameters when redirecting the user to the redirect_uri .

This page will ask the user to sign up or log in with their Twitch account and allow them to choose whether to authorize your application or not.

2.If the user authorizes your application, they will be redirected to the following URL:
https://[your registered redirect URI]/?code=[CODE]

I am stuck on part 3:

3.On your server, you can now make the following request to obtain an access token:

POST https://api.twitch.tv/kraken/oauth2/token

POST Body (URL-encoded)
client_id=[your client ID]
&client_secret=[your client secret]
&grant_type=authorization_code
&redirect_uri=[your registered redirect URI]
&code=[code received from redirect URI]
&state=[your provided unique token]

My code is as follows:

            string URI = "https://api.twitch.tv/kraken/oauth2/token";
            string myParameters = "client_id=" + clientId
                    + "&client_secret=" + clientSecret
                    + "&grant_type=authorization_code"
                    + "&redirect_uri=" + Uri.EscapeDataString(redirectUrl) 
                    + "&code= " + codeFromResult;


            var httpClient = new HttpClient();
            var responseFromPost = await httpClient.PostAsync(new Uri(URI), new StringContent(myParameters));

What is contained in responseFromPost is: 'StatusCode: 400, ReasonPhrase: ‘Bad Request’

I am writing a .NET C# application. Any help would be great. Thankyou in advance.

This might be because you’re passing POST data as a string. I would try using FormUrlEncodedContent instead.

1 Like

Hey @night,

Thanks heaps for your help. That worked. Your a legend :smile:

I have changed my code to the following for anyone else they may need help:

            string URIForAuthCode = "https://api.twitch.tv/kraken/oauth2/token";

            List<KeyValuePair<string, string>> paramListForAuthCode = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("client_id", clientId),
                new KeyValuePair<string, string>("client_secret", clientSecret),
                new KeyValuePair<string, string>("grant_type", "authorization_code"),
                new KeyValuePair<string, string>("redirect_uri", redirectUrl),
                new KeyValuePair<string, string>("code", codeFromResult)
            };

            var httpClient = new HttpClient();
            var responseFromPost = await httpClient.PostAsync(new Uri(URIForAuthCode), new FormUrlEncodedContent(paramListForAuthCode));

            string content = await responseFromPost.Content.ReadAsStringAsync();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.