mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Basic implementation of OAuthListener
This commit is contained in:
parent
a9782a5112
commit
4ae7cc56a9
6 changed files with 80 additions and 19 deletions
|
@ -57,16 +57,16 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
|
||||||
|
|
||||||
public sealed class YouTube : FileUploader, IOAuth2
|
public sealed class YouTube : FileUploader, IOAuth2
|
||||||
{
|
{
|
||||||
public OAuth2Info AuthInfo => googleAuth.AuthInfo;
|
public OAuth2Info AuthInfo => OAuth2.AuthInfo;
|
||||||
public YouTubeVideoPrivacy PrivacyType { get; set; }
|
public YouTubeVideoPrivacy PrivacyType { get; set; }
|
||||||
public bool UseShortenedLink { get; set; }
|
public bool UseShortenedLink { get; set; }
|
||||||
public bool ShowDialog { get; set; }
|
public bool ShowDialog { get; set; }
|
||||||
|
|
||||||
private GoogleOAuth2 googleAuth;
|
public GoogleOAuth2 OAuth2 { get; private set; }
|
||||||
|
|
||||||
public YouTube(OAuth2Info oauth)
|
public YouTube(OAuth2Info oauth)
|
||||||
{
|
{
|
||||||
googleAuth = new GoogleOAuth2(oauth, this)
|
OAuth2 = new GoogleOAuth2(oauth, this)
|
||||||
{
|
{
|
||||||
Scope = "https://www.googleapis.com/auth/youtube.upload"
|
Scope = "https://www.googleapis.com/auth/youtube.upload"
|
||||||
};
|
};
|
||||||
|
@ -74,22 +74,22 @@ public YouTube(OAuth2Info oauth)
|
||||||
|
|
||||||
public bool RefreshAccessToken()
|
public bool RefreshAccessToken()
|
||||||
{
|
{
|
||||||
return googleAuth.RefreshAccessToken();
|
return OAuth2.RefreshAccessToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CheckAuthorization()
|
public bool CheckAuthorization()
|
||||||
{
|
{
|
||||||
return googleAuth.CheckAuthorization();
|
return OAuth2.CheckAuthorization();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetAuthorizationURL()
|
public string GetAuthorizationURL()
|
||||||
{
|
{
|
||||||
return googleAuth.GetAuthorizationURL();
|
return OAuth2.GetAuthorizationURL();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool GetAccessToken(string code)
|
public bool GetAccessToken(string code)
|
||||||
{
|
{
|
||||||
return googleAuth.GetAccessToken(code);
|
return OAuth2.GetAccessToken(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override UploadResult Upload(Stream stream, string fileName)
|
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);
|
string metadata = JsonConvert.SerializeObject(uploadVideo);
|
||||||
|
|
||||||
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/youtube/v3/videos?part=id,snippet,status", stream, fileName, "file",
|
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))
|
if (!string.IsNullOrEmpty(result.Response))
|
||||||
{
|
{
|
||||||
|
|
|
@ -3041,10 +3041,17 @@ private void txtGfycatTitle_TextChanged(object sender, EventArgs e)
|
||||||
|
|
||||||
#region YouTube
|
#region YouTube
|
||||||
|
|
||||||
private void oauth2YouTube_OpenButtonClicked()
|
private async void oauth2YouTube_OpenButtonClicked()
|
||||||
{
|
{
|
||||||
OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret);
|
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)
|
private void oauth2YouTube_CompleteButtonClicked(string code)
|
||||||
|
|
|
@ -30,15 +30,15 @@ You should have received a copy of the GNU General Public License
|
||||||
|
|
||||||
namespace ShareX.UploadersLib
|
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 AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
|
||||||
private const string TokenEndpoint = "https://oauth2.googleapis.com/token";
|
private const string TokenEndpoint = "https://oauth2.googleapis.com/token";
|
||||||
private const string UserInfoEndpoint = "https://www.googleapis.com/oauth2/v3/userinfo";
|
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; }
|
public OAuth2Info AuthInfo { get; private set; }
|
||||||
private Uploader GoogleUploader { get; set; }
|
private Uploader GoogleUploader { get; set; }
|
||||||
|
public string RedirectURI { get; set; }
|
||||||
public string Scope { get; set; }
|
public string Scope { get; set; }
|
||||||
|
|
||||||
public GoogleOAuth2(OAuth2Info oauth, Uploader uploader)
|
public GoogleOAuth2(OAuth2Info oauth, Uploader uploader)
|
||||||
|
@ -52,7 +52,7 @@ public string GetAuthorizationURL()
|
||||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||||
args.Add("response_type", "code");
|
args.Add("response_type", "code");
|
||||||
args.Add("client_id", AuthInfo.Client_ID);
|
args.Add("client_id", AuthInfo.Client_ID);
|
||||||
args.Add("redirect_uri", RedirectMethod);
|
args.Add("redirect_uri", RedirectURI);
|
||||||
args.Add("scope", Scope);
|
args.Add("scope", Scope);
|
||||||
|
|
||||||
return URLHelpers.CreateQueryString(AuthorizationEndpoint, args);
|
return URLHelpers.CreateQueryString(AuthorizationEndpoint, args);
|
||||||
|
@ -64,7 +64,7 @@ public bool GetAccessToken(string code)
|
||||||
args.Add("code", code);
|
args.Add("code", code);
|
||||||
args.Add("client_id", AuthInfo.Client_ID);
|
args.Add("client_id", AuthInfo.Client_ID);
|
||||||
args.Add("client_secret", AuthInfo.Client_Secret);
|
args.Add("client_secret", AuthInfo.Client_Secret);
|
||||||
args.Add("redirect_uri", RedirectMethod);
|
args.Add("redirect_uri", RedirectURI);
|
||||||
args.Add("grant_type", "authorization_code");
|
args.Add("grant_type", "authorization_code");
|
||||||
|
|
||||||
string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args);
|
string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args);
|
||||||
|
|
32
ShareX.UploadersLib/OAuth/IOauth2Loopback.cs
Normal file
32
ShareX.UploadersLib/OAuth/IOauth2Loopback.cs
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#endregion License Information (GPL v3)
|
||||||
|
|
||||||
|
namespace ShareX.UploadersLib
|
||||||
|
{
|
||||||
|
public interface IOAuth2Loopback : IOAuth2
|
||||||
|
{
|
||||||
|
string RedirectURI { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,14 +35,32 @@ namespace ShareX.UploadersLib
|
||||||
{
|
{
|
||||||
public class OAuthListener
|
public class OAuthListener
|
||||||
{
|
{
|
||||||
public IOAuth2 OAuth { get; private set; }
|
public IOAuth2Loopback OAuth { get; private set; }
|
||||||
|
|
||||||
public async Task<string> ConnectAsync()
|
public OAuthListener(IOAuth2Loopback oauth)
|
||||||
|
{
|
||||||
|
OAuth = oauth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
IPAddress ip = IPAddress.Loopback;
|
IPAddress ip = IPAddress.Loopback;
|
||||||
int port = URLHelpers.GetRandomUnusedPort();
|
int port = URLHelpers.GetRandomUnusedPort();
|
||||||
string redirectURI = string.Format($"http://{ip}:{port}/");
|
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
|
try
|
||||||
{
|
{
|
||||||
|
@ -79,7 +97,10 @@ public async Task<string> ConnectAsync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return code;
|
if (!string.IsNullOrEmpty(code))
|
||||||
|
{
|
||||||
|
return await Task.Run(() => OAuth.GetAccessToken(code));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -87,7 +108,7 @@ public async Task<string> ConnectAsync()
|
||||||
DebugHelper.WriteException(e);
|
DebugHelper.WriteException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -256,6 +256,7 @@
|
||||||
<Compile Include="Helpers\RequestHelpers.cs" />
|
<Compile Include="Helpers\RequestHelpers.cs" />
|
||||||
<Compile Include="Helpers\UploaderErrorManager.cs" />
|
<Compile Include="Helpers\UploaderErrorManager.cs" />
|
||||||
<Compile Include="OAuth\GoogleOAuth2.cs" />
|
<Compile Include="OAuth\GoogleOAuth2.cs" />
|
||||||
|
<Compile Include="OAuth\IOauth2Loopback.cs" />
|
||||||
<Compile Include="OAuth\IOAuthBase.cs" />
|
<Compile Include="OAuth\IOAuthBase.cs" />
|
||||||
<Compile Include="OAuth\OAuth2ProofKey.cs" />
|
<Compile Include="OAuth\OAuth2ProofKey.cs" />
|
||||||
<Compile Include="Helpers\SSLBypassHelper.cs" />
|
<Compile Include="Helpers\SSLBypassHelper.cs" />
|
||||||
|
|
Loading…
Reference in a new issue