twitch-agent/OAuthTokenGetter.cs

40 lines
1.3 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
public class OAuthTokenGetter
{
public static OAuthToken DoIt(HttpClient client, string clientId, string clientSecret, string[] scopes = null)
{
var scopeString = "";
if (scopes != null && scopes.Length > 0)
{
scopeString = "&scope=" + string.Join(' ', scopes);
}
var postURI = $"https://id.twitch.tv/oauth2/token?client_id={clientId}&client_secret={clientSecret}&grant_type=client_credentials{scopeString}";
//Console.WriteLine(postURI);
var r = client.PostAsync(postURI, null).Result;
var response = "";
using (var streamReader = new StreamReader(r.Content.ReadAsStream()))
{
response = streamReader.ReadToEnd();
}
if(!r.IsSuccessStatusCode)
{
Console.Error.WriteLine(response);
throw new Exception(response);
}
//Console.WriteLine(response);
return JsonConvert.DeserializeObject<OAuthToken>(response);
}
}
public class OAuthToken
{
public string access_token { get; set; }
public string refresh_token { get; set; }
public int expires_in { get; set; }
public string[] scope { get; set; }
public string token_type { get; set; }
}