diff --git a/YouTube.API.Test/WatchLaterTest.cs b/YouTube.API.Test/WatchLaterTest.cs index 8849549..df810c0 100644 --- a/YouTube.API.Test/WatchLaterTest.cs +++ b/YouTube.API.Test/WatchLaterTest.cs @@ -21,9 +21,9 @@ namespace YouTube.API.Test { var task = AuthorizationHelpers.ExchangeToken(new ClientSecrets { - ClientId = "1096685398208-u95rcpkqb4e1ijfmb8jdq3jsg37l8igv.apps.googleusercontent.com", - ClientSecret = "IU5bbdjwvmx8ttJoXQ7e6JWd" - }, "4/twFMhT4xSaAxls-rEayp8MxFI2Oy0knUdDbAXKnfyMkbDHaNyqhV6uM"); + ClientId = "CLIENT_ID", + ClientSecret = "CLIENT_SECRET" + }, "SUCCESS_CODE"); task.Wait(); UserCredential credential = task.Result; diff --git a/YouTube.API/Authorization/AuthorizationHelpers.cs b/YouTube.API/Authorization/AuthorizationHelpers.cs index 07513ba..d479d7c 100644 --- a/YouTube.API/Authorization/AuthorizationHelpers.cs +++ b/YouTube.API/Authorization/AuthorizationHelpers.cs @@ -14,13 +14,14 @@ namespace YouTube.Authorization public static Uri Endpoint => "https://accounts.google.com/o/oauth2/approval".ToUri(); 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, Uri redirectUri, params string[] scopes) + public static Uri FormQueryString(ClientSecrets clientSecrets, 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={redirectUri.AbsoluteUri}&response_type=code&scope={scopeStr}".ToUri(); + return $"https://accounts.google.com/o/oauth2/auth?client_id={clientId}&redirect_uri={redirectUrl}&response_type=code&scope={scopeStr}".ToUri(); } public static async Task ExchangeToken(ClientSecrets clientSecrets, string responseToken) @@ -30,7 +31,7 @@ namespace YouTube.Authorization Dictionary requestBody = new Dictionary { { "code", responseToken }, - //{ "redirect_uri", redirectUrl }, + { "redirect_uri", redirectUrl }, { "grant_type", "authorization_code" }, { "client_id", clientSecrets.ClientId }, { "client_secret", clientSecrets.ClientSecret } @@ -39,7 +40,7 @@ namespace YouTube.Authorization HttpResponseMessage response = await client.PostAsync(tokenEndpoint, new FormUrlEncodedContent(requestBody)); if (!response.IsSuccessStatusCode) - return null; + throw new Exception(await response.Content.ReadAsStringAsync()); string responseString = await response.Content.ReadAsStringAsync(); dynamic responseData = JsonConvert.DeserializeObject(responseString); @@ -79,7 +80,7 @@ namespace YouTube.Authorization HttpResponseMessage response = await client.PostAsync(refreshEndpoint, new FormUrlEncodedContent(requestBody)); if (!response.IsSuccessStatusCode) - return null; + throw new Exception(await response.Content.ReadAsStringAsync()); string responseString = await response.Content.ReadAsStringAsync(); dynamic responseData = JsonConvert.DeserializeObject(responseString);