diff --git a/ShareX.UploadersLib/FileUploaders/YouTube.cs b/ShareX.UploadersLib/FileUploaders/YouTube.cs index ce5125cda..95806d40f 100644 --- a/ShareX.UploadersLib/FileUploaders/YouTube.cs +++ b/ShareX.UploadersLib/FileUploaders/YouTube.cs @@ -57,16 +57,16 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer public sealed class YouTube : FileUploader, IOAuth2 { - public OAuth2Info AuthInfo => googleAuth.AuthInfo; + public OAuth2Info AuthInfo => OAuth2.AuthInfo; public YouTubeVideoPrivacy PrivacyType { get; set; } public bool UseShortenedLink { get; set; } public bool ShowDialog { get; set; } - private GoogleOAuth2 googleAuth; + public GoogleOAuth2 OAuth2 { get; private set; } public YouTube(OAuth2Info oauth) { - googleAuth = new GoogleOAuth2(oauth, this) + OAuth2 = new GoogleOAuth2(oauth, this) { Scope = "https://www.googleapis.com/auth/youtube.upload" }; @@ -74,22 +74,22 @@ public YouTube(OAuth2Info oauth) public bool RefreshAccessToken() { - return googleAuth.RefreshAccessToken(); + return OAuth2.RefreshAccessToken(); } public bool CheckAuthorization() { - return googleAuth.CheckAuthorization(); + return OAuth2.CheckAuthorization(); } public string GetAuthorizationURL() { - return googleAuth.GetAuthorizationURL(); + return OAuth2.GetAuthorizationURL(); } public bool GetAccessToken(string code) { - return googleAuth.GetAccessToken(code); + return OAuth2.GetAccessToken(code); } public override UploadResult Upload(Stream stream, string fileName) @@ -133,7 +133,7 @@ public override UploadResult Upload(Stream stream, string fileName) string metadata = JsonConvert.SerializeObject(uploadVideo); UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName, "file", - headers: googleAuth.GetAuthHeaders(), relatedData: metadata); + headers: OAuth2.GetAuthHeaders(), relatedData: metadata); if (!string.IsNullOrEmpty(result.Response)) { diff --git a/ShareX.UploadersLib/Forms/UploadersConfigForm.cs b/ShareX.UploadersLib/Forms/UploadersConfigForm.cs index 9dd9a3745..8f26614e6 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigForm.cs +++ b/ShareX.UploadersLib/Forms/UploadersConfigForm.cs @@ -3041,10 +3041,17 @@ private void txtGfycatTitle_TextChanged(object sender, EventArgs e) #region YouTube - private void oauth2YouTube_OpenButtonClicked() + private async void oauth2YouTube_OpenButtonClicked() { OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret); - Config.YouTubeOAuth2Info = OAuth2Open(new YouTube(oauth)); + GoogleOAuth2 oauthGoogle = new YouTube(oauth).OAuth2; + OAuthListener listener = new OAuthListener(oauthGoogle); + bool result = await listener.ConnectAsync(); + if (result) + { + Config.YouTubeOAuth2Info = listener.OAuth.AuthInfo; + } + ConfigureOAuthStatus(oauth2YouTube, result); } private void oauth2YouTube_CompleteButtonClicked(string code) diff --git a/ShareX.UploadersLib/OAuth/GoogleOAuth2.cs b/ShareX.UploadersLib/OAuth/GoogleOAuth2.cs index 9cf285ec8..3de83e4e3 100644 --- a/ShareX.UploadersLib/OAuth/GoogleOAuth2.cs +++ b/ShareX.UploadersLib/OAuth/GoogleOAuth2.cs @@ -30,15 +30,15 @@ namespace ShareX.UploadersLib { - public class GoogleOAuth2 : IOAuth2 + public class GoogleOAuth2 : IOAuth2Loopback { private const string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"; private const string TokenEndpoint = "https://oauth2.googleapis.com/token"; private const string UserInfoEndpoint = "https://www.googleapis.com/oauth2/v3/userinfo"; - private const string RedirectMethod = "urn:ietf:wg:oauth:2.0:oob"; // Manual copy-paste method public OAuth2Info AuthInfo { get; private set; } private Uploader GoogleUploader { get; set; } + public string RedirectURI { get; set; } public string Scope { get; set; } public GoogleOAuth2(OAuth2Info oauth, Uploader uploader) @@ -52,7 +52,7 @@ public string GetAuthorizationURL() Dictionary args = new Dictionary(); args.Add("response_type", "code"); args.Add("client_id", AuthInfo.Client_ID); - args.Add("redirect_uri", RedirectMethod); + args.Add("redirect_uri", RedirectURI); args.Add("scope", Scope); return URLHelpers.CreateQueryString(AuthorizationEndpoint, args); @@ -64,7 +64,7 @@ public bool GetAccessToken(string code) args.Add("code", code); args.Add("client_id", AuthInfo.Client_ID); args.Add("client_secret", AuthInfo.Client_Secret); - args.Add("redirect_uri", RedirectMethod); + args.Add("redirect_uri", RedirectURI); args.Add("grant_type", "authorization_code"); string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args); diff --git a/ShareX.UploadersLib/OAuth/IOauth2Loopback.cs b/ShareX.UploadersLib/OAuth/IOauth2Loopback.cs new file mode 100644 index 000000000..eda2c7a81 --- /dev/null +++ b/ShareX.UploadersLib/OAuth/IOauth2Loopback.cs @@ -0,0 +1,32 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2022 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +namespace ShareX.UploadersLib +{ + public interface IOAuth2Loopback : IOAuth2 + { + string RedirectURI { get; set; } + } +} \ No newline at end of file diff --git a/ShareX.UploadersLib/OAuth/OAuthListener.cs b/ShareX.UploadersLib/OAuth/OAuthListener.cs index 8423cf439..106128aee 100644 --- a/ShareX.UploadersLib/OAuth/OAuthListener.cs +++ b/ShareX.UploadersLib/OAuth/OAuthListener.cs @@ -35,14 +35,32 @@ namespace ShareX.UploadersLib { public class OAuthListener { - public IOAuth2 OAuth { get; private set; } + public IOAuth2Loopback OAuth { get; private set; } - public async Task ConnectAsync() + public OAuthListener(IOAuth2Loopback oauth) + { + OAuth = oauth; + } + + public async Task ConnectAsync() { IPAddress ip = IPAddress.Loopback; int port = URLHelpers.GetRandomUnusedPort(); string redirectURI = string.Format($"http://{ip}:{port}/"); - URLHelpers.OpenURL(redirectURI + "?code=test"); + + OAuth.RedirectURI = redirectURI; + string url = OAuth.GetAuthorizationURL(); + + if (!string.IsNullOrEmpty(url)) + { + URLHelpers.OpenURL(url); + DebugHelper.WriteLine("Authorization URL is opened: " + url); + } + else + { + DebugHelper.WriteLine("Authorization URL is empty."); + return false; + } try { @@ -79,7 +97,10 @@ public async Task ConnectAsync() } } - return code; + if (!string.IsNullOrEmpty(code)) + { + return await Task.Run(() => OAuth.GetAccessToken(code)); + } } } catch (Exception e) @@ -87,7 +108,7 @@ public async Task ConnectAsync() DebugHelper.WriteException(e); } - return null; + return false; } } } \ No newline at end of file diff --git a/ShareX.UploadersLib/ShareX.UploadersLib.csproj b/ShareX.UploadersLib/ShareX.UploadersLib.csproj index 79a381ed8..d0c26416b 100644 --- a/ShareX.UploadersLib/ShareX.UploadersLib.csproj +++ b/ShareX.UploadersLib/ShareX.UploadersLib.csproj @@ -256,6 +256,7 @@ +