[SOLVED] Different API responses when using C#

Ha! This actually seems to have fixed it. Here’s a small console application showcasing it:

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Temp
{
    class Program
    {
        public static async Task<string> RefreshToken()
        {
            using (var client = new HttpClient())
            {
                var post = new HttpRequestMessage(HttpMethod.Post, "https://id.twitch.tv/oauth2/token");
                var body = new
                {
                    client_id = "kojortd5rhu0bzoamqm6zlsh0mewsu",
                    grant_type = "refresh_token",
                    client_secret = "none",
                    refresh_token = "none"
                };
                var json = JsonConvert.SerializeObject(body);
                post.Content = new StringContent(json, Encoding.UTF8, "application/json");
                post.Content.Headers.ContentType.CharSet = ""; // THE FIX
                var responseMessage = await client.SendAsync(post);
                var response = await responseMessage.Content.ReadAsStringAsync();
                return response;
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine(RefreshToken().Result);
            Console.ReadKey();
        }
    }
}

That one commented line is all it needed. Unfortunately, I don’t think there’s a way to specify that the ContentType is application/json without specifying the charset. So that extra line is needed.

Anyway, I wouldn’t have thought that that mattered. Thank you very much, 3v!

1 Like