146 lines
5.9 KiB
C#
146 lines
5.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Threading;
|
|
|
|
using Google.Apis.Auth.OAuth2;
|
|
using Google.Apis.Auth.OAuth2.Flows;
|
|
using Google.Apis.Services;
|
|
using Google.Apis.YouTube.v3;
|
|
using Google.Apis.YouTube.v3.Data;
|
|
using Windows.Storage;
|
|
|
|
namespace FoxTube
|
|
{
|
|
public class SecretsVault
|
|
{
|
|
#region Static Information
|
|
public static NetworkCredential EmailCredential => new NetworkCredential("youwillneverknowthisadress@gmail.com", "thisisthepassword12345");
|
|
public static ClientSecrets Secrets => new ClientSecrets()
|
|
{
|
|
ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com",
|
|
ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_"
|
|
};
|
|
|
|
public static SecretsVault Vault => Methods.MainPage.Vault;
|
|
|
|
public static string AccountId => Methods.MainPage.Vault.userId;
|
|
public static bool IsAuthorized => Vault.IsLoged;
|
|
|
|
public static Google.Apis.YouTube.v3.Data.Channel UserChannel => Methods.MainPage.Vault.channel;
|
|
public static List<PlaylistItem> WatchLater => Methods.MainPage.Vault.later;
|
|
public static List<PlaylistItem> UserHistory => Methods.MainPage.Vault.history;
|
|
public static List<Subscription> Subscriptions => Methods.MainPage.Vault.subs;
|
|
|
|
public static YouTubeService NoAuthService => new YouTubeService(new BaseClientService.Initializer()
|
|
{
|
|
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
|
|
ApplicationName = "FoxTube"
|
|
});
|
|
|
|
public static YouTubeService Service => new YouTubeService(new BaseClientService.Initializer()
|
|
{
|
|
HttpClientInitializer = Vault.Credential,
|
|
ApplicationName = "FoxTube"
|
|
});
|
|
#endregion
|
|
|
|
#region Object containers
|
|
public bool IsLoged = false;
|
|
public string userId;
|
|
public List<PlaylistItem> history = new List<PlaylistItem>();
|
|
public List<Subscription> subs = new List<Subscription>();
|
|
public List<PlaylistItem> later = new List<PlaylistItem>();
|
|
public Google.Apis.YouTube.v3.Data.Channel channel;
|
|
public UserCredential Credential;
|
|
public event EventHandler AuthorizationStateChanged;
|
|
private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
|
|
|
|
public async void Authorize()
|
|
{
|
|
try { Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); }
|
|
catch { }
|
|
if(Credential != null)
|
|
{
|
|
if (settings.Values["authorized"] == null)
|
|
settings.Values.Add("authorized", true);
|
|
else settings.Values["authorized"] = true;
|
|
IsLoged = true;
|
|
|
|
var request = Service.Channels.List("snippet,contentDetails");
|
|
request.Mine = true;
|
|
channel = (await request.ExecuteAsync()).Items[0];
|
|
userId = channel.Id;
|
|
|
|
PlaylistItemsResource.ListRequest playlistRequest = Service.PlaylistItems.List("snippet");
|
|
playlistRequest.PlaylistId = channel.ContentDetails.RelatedPlaylists.WatchHistory;
|
|
playlistRequest.MaxResults = 50;
|
|
PlaylistItemListResponse playlistResponse = await playlistRequest.ExecuteAsync();
|
|
history.Clear();
|
|
foreach (PlaylistItem i in playlistResponse.Items)
|
|
history.Add(i);
|
|
|
|
playlistRequest = Service.PlaylistItems.List("snippet");
|
|
playlistRequest.PlaylistId = channel.ContentDetails.RelatedPlaylists.WatchLater;
|
|
playlistRequest.MaxResults = 50;
|
|
playlistResponse = await playlistRequest.ExecuteAsync();
|
|
later.Clear();
|
|
|
|
foreach (PlaylistItem i in playlistResponse.Items)
|
|
later.Add(i);
|
|
|
|
string nextToken = playlistResponse.NextPageToken;
|
|
while (nextToken != null)
|
|
{
|
|
playlistRequest.PageToken = nextToken;
|
|
playlistResponse = await playlistRequest.ExecuteAsync();
|
|
foreach (PlaylistItem i in playlistResponse.Items)
|
|
later.Add(i);
|
|
|
|
nextToken = playlistResponse.NextPageToken;
|
|
}
|
|
|
|
SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet");
|
|
subRequest.Mine = true;
|
|
subRequest.MaxResults = 50;
|
|
subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance;
|
|
SubscriptionListResponse subResponse = await subRequest.ExecuteAsync();
|
|
subs.Clear();
|
|
|
|
foreach (Subscription s in subResponse.Items)
|
|
subs.Add(s);
|
|
|
|
nextToken = subResponse.NextPageToken;
|
|
while(nextToken != null)
|
|
{
|
|
subRequest.PageToken = nextToken;
|
|
subResponse = await subRequest.ExecuteAsync();
|
|
foreach (Subscription s in subResponse.Items)
|
|
subs.Add(s);
|
|
}
|
|
|
|
AuthorizationStateChanged.Invoke(this, null);
|
|
}
|
|
}
|
|
|
|
public async void Deauthenticate()
|
|
{
|
|
if(await Credential.RevokeTokenAsync(CancellationToken.None))
|
|
{
|
|
Credential = null;
|
|
AuthorizationStateChanged.Invoke(this, null);
|
|
settings.Values["authorized"] = false;
|
|
}
|
|
}
|
|
|
|
public void CheckAuthorization()
|
|
{
|
|
if (settings.Values["authorized"] == null || !(bool)settings.Values["authorized"])
|
|
IsLoged = false;
|
|
else
|
|
Authorize();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|