Updated authorization helpers and main class name
This commit is contained in:
Binary file not shown.
@@ -9,7 +9,7 @@ namespace YouTube.API.Test
|
|||||||
[Test]
|
[Test]
|
||||||
public void ValidCaptionsTest()
|
public void ValidCaptionsTest()
|
||||||
{
|
{
|
||||||
YouTubeService service = new YouTubeService();
|
ExtendedYouTubeService service = new ExtendedYouTubeService();
|
||||||
ClosedCaptionInfo info = service.VideoPlayback.List("VC5-YkjMHuw").Execute().ClosedCaptions.FirstOrDefault();
|
ClosedCaptionInfo info = service.VideoPlayback.List("VC5-YkjMHuw").Execute().ClosedCaptions.FirstOrDefault();
|
||||||
ClosedCaptionTrack track = service.Captions.Load(info).Execute();
|
ClosedCaptionTrack track = service.Captions.Load(info).Execute();
|
||||||
Assert.IsNotNull(track);
|
Assert.IsNotNull(track);
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace YouTube.API.Test
|
|||||||
[Test]
|
[Test]
|
||||||
public void ValidManifestTest()
|
public void ValidManifestTest()
|
||||||
{
|
{
|
||||||
YouTubeService service = new YouTubeService();
|
ExtendedYouTubeService service = new ExtendedYouTubeService();
|
||||||
IReadOnlyList<DashManifest> manifests = service.DashManifests.List("NkGbcQwWxqk").Execute();
|
IReadOnlyList<DashManifest> manifests = service.DashManifests.List("NkGbcQwWxqk").Execute();
|
||||||
foreach (var i in manifests)
|
foreach (var i in manifests)
|
||||||
Console.WriteLine(i.Label);
|
Console.WriteLine(i.Label);
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace YouTube.API.Test
|
|||||||
[Test]
|
[Test]
|
||||||
public void ValidVideoPlaybackTest()
|
public void ValidVideoPlaybackTest()
|
||||||
{
|
{
|
||||||
YouTubeService service = new YouTubeService();
|
ExtendedYouTubeService service = new ExtendedYouTubeService();
|
||||||
VideoPlayback info = service.VideoPlayback.List("VC5-YkjMHuw").Execute();
|
VideoPlayback info = service.VideoPlayback.List("VC5-YkjMHuw").Execute();
|
||||||
Assert.NotNull(info);
|
Assert.NotNull(info);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace YouTube.API.Test
|
|||||||
public class WatchLaterTest
|
public class WatchLaterTest
|
||||||
{
|
{
|
||||||
const string testVideoId = "NkGbcQwWxqk";
|
const string testVideoId = "NkGbcQwWxqk";
|
||||||
YouTubeService service;
|
ExtendedYouTubeService service;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
@@ -27,7 +27,7 @@ namespace YouTube.API.Test
|
|||||||
task.Wait();
|
task.Wait();
|
||||||
|
|
||||||
UserCredential credential = task.Result;
|
UserCredential credential = task.Result;
|
||||||
service = new YouTubeService(new BaseClientService.Initializer
|
service = new ExtendedYouTubeService(new BaseClientService.Initializer
|
||||||
{
|
{
|
||||||
HttpClientInitializer = credential,
|
HttpClientInitializer = credential,
|
||||||
ApplicationName = "FoxTube"
|
ApplicationName = "FoxTube"
|
||||||
|
|||||||
@@ -16,17 +16,15 @@ namespace YouTube.Authorization
|
|||||||
public static class AuthorizationHelpers
|
public static class AuthorizationHelpers
|
||||||
{
|
{
|
||||||
public static Uri Endpoint => "https://accounts.google.com/o/oauth2/approval".ToUri();
|
public static Uri Endpoint => "https://accounts.google.com/o/oauth2/approval".ToUri();
|
||||||
public static string RedirectUrl => Uri.EscapeDataString(redirectUrl);
|
const string refreshEndpoint = "https://oauth2.googleapis.com/token";
|
||||||
|
|
||||||
const string tokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
|
const string tokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
|
||||||
const string redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
|
|
||||||
|
|
||||||
public static Uri FormQueryString(ClientSecrets clientSecrets, params string[] scopes)
|
public static Uri FormQueryString(ClientSecrets clientSecrets, Uri redirectUri, params string[] scopes)
|
||||||
{
|
{
|
||||||
string clientId = Uri.EscapeDataString(clientSecrets.ClientId);
|
string clientId = Uri.EscapeDataString(clientSecrets.ClientId);
|
||||||
string scopeStr = string.Join(' ', scopes);
|
string scopeStr = string.Join(' ', scopes);
|
||||||
|
|
||||||
return $"https://accounts.google.com/o/oauth2/auth?client_id={clientId}&redirect_uri={RedirectUrl}&response_type=code&scope={scopeStr}".ToUri();
|
return $"https://accounts.google.com/o/oauth2/auth?client_id={clientId}&redirect_uri={redirectUri.AbsoluteUri}&response_type=code&scope={scopeStr}".ToUri();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<UserCredential> ExchangeToken(ClientSecrets clientSecrets, string responseToken)
|
public static async Task<UserCredential> ExchangeToken(ClientSecrets clientSecrets, string responseToken)
|
||||||
@@ -36,7 +34,7 @@ namespace YouTube.Authorization
|
|||||||
Dictionary<string, string> requestBody = new Dictionary<string, string>
|
Dictionary<string, string> requestBody = new Dictionary<string, string>
|
||||||
{
|
{
|
||||||
{ "code", responseToken },
|
{ "code", responseToken },
|
||||||
{ "redirect_uri", redirectUrl },
|
//{ "redirect_uri", redirectUrl },
|
||||||
{ "grant_type", "authorization_code" },
|
{ "grant_type", "authorization_code" },
|
||||||
{ "client_id", clientSecrets.ClientId },
|
{ "client_id", clientSecrets.ClientId },
|
||||||
{ "client_secret", clientSecrets.ClientSecret }
|
{ "client_secret", clientSecrets.ClientSecret }
|
||||||
@@ -59,13 +57,53 @@ namespace YouTube.Authorization
|
|||||||
TokenType = responseData.token_type
|
TokenType = responseData.token_type
|
||||||
};
|
};
|
||||||
|
|
||||||
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer()
|
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
||||||
{
|
{
|
||||||
ClientSecrets = clientSecrets,
|
ClientSecrets = clientSecrets,
|
||||||
Scopes = responseData.scope.ToString().Split(' ')
|
Scopes = responseData.scope.ToString().Split(' ')
|
||||||
});
|
});
|
||||||
|
|
||||||
return new UserCredential(authorizationCodeFlow, "user", tokenResponse);
|
UserCredential credential = new UserCredential(authorizationCodeFlow, "user", tokenResponse);
|
||||||
|
|
||||||
|
return credential;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<UserCredential> RestoreUser(ClientSecrets clientSecrets, string refreshToken)
|
||||||
|
{
|
||||||
|
using HttpClient client = new HttpClient();
|
||||||
|
|
||||||
|
Dictionary<string, string> requestBody = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "client_id", clientSecrets.ClientId },
|
||||||
|
{ "client_secret", clientSecrets.ClientSecret },
|
||||||
|
{ "refresh_token", refreshToken },
|
||||||
|
{ "grant_type", "refresh_token" }
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.PostAsync(refreshEndpoint, new FormUrlEncodedContent(requestBody));
|
||||||
|
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
string responseString = await response.Content.ReadAsStringAsync();
|
||||||
|
dynamic responseData = JsonConvert.DeserializeObject(responseString);
|
||||||
|
|
||||||
|
TokenResponse tokenResponse = new TokenResponse
|
||||||
|
{
|
||||||
|
AccessToken = responseData.access_token,
|
||||||
|
ExpiresInSeconds = responseData.expires_in,
|
||||||
|
RefreshToken = refreshToken,
|
||||||
|
TokenType = responseData.token_type
|
||||||
|
};
|
||||||
|
|
||||||
|
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
||||||
|
{
|
||||||
|
ClientSecrets = clientSecrets
|
||||||
|
});
|
||||||
|
|
||||||
|
UserCredential credential = new UserCredential(authorizationCodeFlow, "user", tokenResponse);
|
||||||
|
|
||||||
|
return credential;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace YouTube
|
namespace YouTube
|
||||||
{
|
{
|
||||||
public partial class YouTubeService : Google.Apis.YouTube.v3.YouTubeService
|
public partial class ExtendedYouTubeService : Google.Apis.YouTube.v3.YouTubeService
|
||||||
{
|
{
|
||||||
public DashManifestsResource DashManifests => new DashManifestsResource(this);
|
public DashManifestsResource DashManifests => new DashManifestsResource(this);
|
||||||
public VideoPlaybackResource VideoPlayback => new VideoPlaybackResource(this);
|
public VideoPlaybackResource VideoPlayback => new VideoPlaybackResource(this);
|
||||||
@@ -11,8 +11,8 @@ namespace YouTube
|
|||||||
public WatchLaterResource WatchLater => new WatchLaterResource(this);
|
public WatchLaterResource WatchLater => new WatchLaterResource(this);
|
||||||
// TODO: Add Activities override for recomendations and subscriptions
|
// TODO: Add Activities override for recomendations and subscriptions
|
||||||
|
|
||||||
public YouTubeService() : base() { }
|
public ExtendedYouTubeService() : base() { }
|
||||||
|
|
||||||
public YouTubeService(Initializer initializer) : base(initializer) { }
|
public ExtendedYouTubeService(Initializer initializer) : base(initializer) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user