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]
|
||||
public void ValidCaptionsTest()
|
||||
{
|
||||
YouTubeService service = new YouTubeService();
|
||||
ExtendedYouTubeService service = new ExtendedYouTubeService();
|
||||
ClosedCaptionInfo info = service.VideoPlayback.List("VC5-YkjMHuw").Execute().ClosedCaptions.FirstOrDefault();
|
||||
ClosedCaptionTrack track = service.Captions.Load(info).Execute();
|
||||
Assert.IsNotNull(track);
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace YouTube.API.Test
|
||||
[Test]
|
||||
public void ValidManifestTest()
|
||||
{
|
||||
YouTubeService service = new YouTubeService();
|
||||
ExtendedYouTubeService service = new ExtendedYouTubeService();
|
||||
IReadOnlyList<DashManifest> manifests = service.DashManifests.List("NkGbcQwWxqk").Execute();
|
||||
foreach (var i in manifests)
|
||||
Console.WriteLine(i.Label);
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace YouTube.API.Test
|
||||
[Test]
|
||||
public void ValidVideoPlaybackTest()
|
||||
{
|
||||
YouTubeService service = new YouTubeService();
|
||||
ExtendedYouTubeService service = new ExtendedYouTubeService();
|
||||
VideoPlayback info = service.VideoPlayback.List("VC5-YkjMHuw").Execute();
|
||||
Assert.NotNull(info);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace YouTube.API.Test
|
||||
public class WatchLaterTest
|
||||
{
|
||||
const string testVideoId = "NkGbcQwWxqk";
|
||||
YouTubeService service;
|
||||
ExtendedYouTubeService service;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
@@ -27,7 +27,7 @@ namespace YouTube.API.Test
|
||||
task.Wait();
|
||||
|
||||
UserCredential credential = task.Result;
|
||||
service = new YouTubeService(new BaseClientService.Initializer
|
||||
service = new ExtendedYouTubeService(new BaseClientService.Initializer
|
||||
{
|
||||
HttpClientInitializer = credential,
|
||||
ApplicationName = "FoxTube"
|
||||
|
||||
@@ -16,17 +16,15 @@ namespace YouTube.Authorization
|
||||
public static class AuthorizationHelpers
|
||||
{
|
||||
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 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 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)
|
||||
@@ -36,7 +34,7 @@ namespace YouTube.Authorization
|
||||
Dictionary<string, string> requestBody = new Dictionary<string, string>
|
||||
{
|
||||
{ "code", responseToken },
|
||||
{ "redirect_uri", redirectUrl },
|
||||
//{ "redirect_uri", redirectUrl },
|
||||
{ "grant_type", "authorization_code" },
|
||||
{ "client_id", clientSecrets.ClientId },
|
||||
{ "client_secret", clientSecrets.ClientSecret }
|
||||
@@ -59,13 +57,53 @@ namespace YouTube.Authorization
|
||||
TokenType = responseData.token_type
|
||||
};
|
||||
|
||||
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer()
|
||||
AuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
|
||||
{
|
||||
ClientSecrets = clientSecrets,
|
||||
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
|
||||
{
|
||||
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 VideoPlaybackResource VideoPlayback => new VideoPlaybackResource(this);
|
||||
@@ -11,8 +11,8 @@ namespace YouTube
|
||||
public WatchLaterResource WatchLater => new WatchLaterResource(this);
|
||||
// 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