9103ff1fb7
- MVVM architecture (Models, ViewModels, Views, Services, Helpers) - Models: Scene, Source, StreamConfig, StreamHealth, YouTubeChannel, ChatMessage - Services: YouTubeAuthService (OAuth2), YouTubeStreamService (broadcasts/stream health), YouTubeChatService (live chat polling) - ViewModels: MainViewModel with scene management, stream controls, chat - MainWindow with dark theme: scene/source panel, preview area, chat panel, status bar - Version roadmap: v0.1 (scenes, RTMP, OAuth2, chat, health) -> 1.0
119 lines
4.4 KiB
C#
119 lines
4.4 KiB
C#
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using ytLive.Models;
|
|
|
|
namespace ytLive.Services;
|
|
|
|
/// <summary>
|
|
/// Handles YouTube OAuth2 authentication flow.
|
|
/// Uses a local HTTP listener for the redirect callback.
|
|
/// </summary>
|
|
public class YouTubeAuthService
|
|
{
|
|
private const string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
|
|
private const string TokenEndpoint = "https://oauth2.googleapis.com/token";
|
|
private const string Scope = "https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.force-ssl";
|
|
|
|
private readonly string _clientId;
|
|
private readonly string _clientSecret;
|
|
private readonly HttpClient _http = new();
|
|
|
|
public YouTubeChannel? CurrentChannel { get; private set; }
|
|
|
|
public YouTubeAuthService(string clientId, string clientSecret)
|
|
{
|
|
_clientId = clientId;
|
|
_clientSecret = clientSecret;
|
|
}
|
|
|
|
public string GetAuthorizationUrl(string redirectUri)
|
|
{
|
|
var parameters = HttpUtility.ParseQueryString(string.Empty);
|
|
parameters["client_id"] = _clientId;
|
|
parameters["redirect_uri"] = redirectUri;
|
|
parameters["response_type"] = "code";
|
|
parameters["scope"] = Scope;
|
|
parameters["access_type"] = "offline";
|
|
parameters["prompt"] = "consent";
|
|
|
|
return $"{AuthorizationEndpoint}?{parameters}";
|
|
}
|
|
|
|
public async Task<YouTubeChannel?> ExchangeCodeForToken(string code, string redirectUri)
|
|
{
|
|
var body = new FormUrlEncodedContent(new Dictionary<string, string>
|
|
{
|
|
["code"] = code,
|
|
["client_id"] = _clientId,
|
|
["client_secret"] = _clientSecret,
|
|
["redirect_uri"] = redirectUri,
|
|
["grant_type"] = "authorization_code"
|
|
});
|
|
|
|
var response = await _http.PostAsync(TokenEndpoint, body);
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var tokenData = JsonSerializer.Deserialize<JsonElement>(json);
|
|
|
|
var accessToken = tokenData.GetProperty("access_token").GetString()!;
|
|
var refreshToken = tokenData.GetProperty("refresh_token").GetString()!;
|
|
var expiresIn = tokenData.GetProperty("expires_in").GetInt32();
|
|
|
|
// Fetch channel info
|
|
return await FetchChannelInfo(accessToken, refreshToken, expiresIn);
|
|
}
|
|
|
|
public async Task<bool> RefreshToken()
|
|
{
|
|
if (CurrentChannel == null) return false;
|
|
|
|
var body = new FormUrlEncodedContent(new Dictionary<string, string>
|
|
{
|
|
["refresh_token"] = CurrentChannel.RefreshToken,
|
|
["client_id"] = _clientId,
|
|
["client_secret"] = _clientSecret,
|
|
["grant_type"] = "refresh_token"
|
|
});
|
|
|
|
var response = await _http.PostAsync(TokenEndpoint, body);
|
|
if (!response.IsSuccessStatusCode) return false;
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var tokenData = JsonSerializer.Deserialize<JsonElement>(json);
|
|
|
|
CurrentChannel.AccessToken = tokenData.GetProperty("access_token").GetString()!;
|
|
CurrentChannel.TokenExpiry = DateTime.UtcNow.AddSeconds(tokenData.GetProperty("expires_in").GetInt32());
|
|
return true;
|
|
}
|
|
|
|
private async Task<YouTubeChannel?> FetchChannelInfo(string accessToken, string refreshToken, int expiresIn)
|
|
{
|
|
_http.DefaultRequestHeaders.Authorization = new("Bearer", accessToken);
|
|
var response = await _http.GetAsync("https://www.googleapis.com/youtube/v3/channels?part=snippet&mine=true");
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var data = JsonSerializer.Deserialize<JsonElement>(json);
|
|
var items = data.GetProperty("items");
|
|
|
|
if (items.GetArrayLength() == 0) return null;
|
|
|
|
var snippet = items[0].GetProperty("snippet");
|
|
CurrentChannel = new YouTubeChannel
|
|
{
|
|
ChannelId = items[0].GetProperty("id").GetString()!,
|
|
DisplayName = snippet.GetProperty("title").GetString()!,
|
|
ProfileImageUrl = snippet.GetProperty("thumbnails").GetProperty("default").GetProperty("url").GetString()!,
|
|
AccessToken = accessToken,
|
|
RefreshToken = refreshToken,
|
|
TokenExpiry = DateTime.UtcNow.AddSeconds(expiresIn)
|
|
};
|
|
|
|
return CurrentChannel;
|
|
}
|
|
}
|