Use OAuthLoopbackControl for Google Cloud Storage

This commit is contained in:
Jaex 2022-12-10 15:38:41 +03:00
parent 542df108d1
commit 3382c62c41
5 changed files with 508 additions and 520 deletions

View file

@ -62,6 +62,8 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public sealed class GoogleCloudStorage : FileUploader, IOAuth2
{
public GoogleOAuth2 OAuth2 { get; private set; }
public OAuth2Info AuthInfo => OAuth2.AuthInfo;
public string Bucket { get; set; }
public string Domain { get; set; }
public string Prefix { get; set; }
@ -70,36 +72,32 @@ public sealed class GoogleCloudStorage : FileUploader, IOAuth2
public bool RemoveExtensionVideo { get; set; }
public bool SetPublicACL { get; set; }
public OAuth2Info AuthInfo => googleAuth.AuthInfo;
private GoogleOAuth2 googleAuth;
public GoogleCloudStorage(OAuth2Info oauth)
{
googleAuth = new GoogleOAuth2(oauth, this)
OAuth2 = new GoogleOAuth2(oauth, this)
{
Scope = "https://www.googleapis.com/auth/devstorage.read_write"
Scope = "https://www.googleapis.com/auth/devstorage.read_write https://www.googleapis.com/auth/userinfo.profile"
};
}
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)
@ -129,7 +127,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string serializedGoogleCloudStorageMetadata = JsonConvert.SerializeObject(googleCloudStorageMetadata);
UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart&fields=name", stream, fileName, null, headers: googleAuth.GetAuthHeaders(), contentType: "multipart/related", relatedData: serializedGoogleCloudStorageMetadata);
UploadResult result = SendRequestFile($"https://www.googleapis.com/upload/storage/v1/b/{Bucket}/o?uploadType=multipart&fields=name", stream, fileName, null, headers: OAuth2.GetAuthHeaders(), contentType: "multipart/related", relatedData: serializedGoogleCloudStorageMetadata);
GoogleCloudStorageResponse googleCloudStorageResponse = JsonConvert.DeserializeObject<GoogleCloudStorageResponse>(result.Response);

View file

@ -196,6 +196,7 @@ private void InitializeComponent()
this.cbOneDriveCreateShareableLink = new System.Windows.Forms.CheckBox();
this.oAuth2OneDrive = new ShareX.UploadersLib.OAuthControl();
this.tpGoogleDrive = new System.Windows.Forms.TabPage();
this.oauth2GoogleDrive = new ShareX.UploadersLib.OAuthLoopbackControl();
this.cbGoogleDriveSharedDrive = new System.Windows.Forms.ComboBox();
this.cbGoogleDriveDirectLink = new System.Windows.Forms.CheckBox();
this.cbGoogleDriveUseFolder = new System.Windows.Forms.CheckBox();
@ -271,7 +272,6 @@ private void InitializeComponent()
this.txtGoogleCloudStorageDomain = new System.Windows.Forms.TextBox();
this.lblGoogleCloudStorageBucket = new System.Windows.Forms.Label();
this.txtGoogleCloudStorageBucket = new System.Windows.Forms.TextBox();
this.oauth2GoogleCloudStorage = new ShareX.UploadersLib.OAuthControl();
this.tpAzureStorage = new System.Windows.Forms.TabPage();
this.lblAzureStorageURLPreview = new System.Windows.Forms.Label();
this.lblAzureStorageURLPreviewLabel = new System.Windows.Forms.Label();
@ -630,7 +630,7 @@ private void InitializeComponent()
this.tcUploaders = new System.Windows.Forms.TabControl();
this.tttvMain = new ShareX.HelpersLib.TabToTreeView();
this.actRapidShareAccountType = new ShareX.UploadersLib.AccountTypeControl();
this.oauth2GoogleDrive = new ShareX.UploadersLib.OAuthLoopbackControl();
this.oauth2GoogleCloudStorage = new ShareX.UploadersLib.OAuthLoopbackControl();
this.tpOtherUploaders.SuspendLayout();
this.tcOtherUploaders.SuspendLayout();
this.tpTwitter.SuspendLayout();
@ -1784,6 +1784,14 @@ private void InitializeComponent()
resources.ApplyResources(this.tpGoogleDrive, "tpGoogleDrive");
this.tpGoogleDrive.Name = "tpGoogleDrive";
//
// oauth2GoogleDrive
//
resources.ApplyResources(this.oauth2GoogleDrive, "oauth2GoogleDrive");
this.oauth2GoogleDrive.Name = "oauth2GoogleDrive";
this.oauth2GoogleDrive.UserInfo = null;
this.oauth2GoogleDrive.ConnectButtonClicked += new System.Action(this.oauth2GoogleDrive_ConnectButtonClicked);
this.oauth2GoogleDrive.DisconnectButtonClicked += new System.Action(this.oauth2GoogleDrive_DisconnectButtonClicked);
//
// cbGoogleDriveSharedDrive
//
this.cbGoogleDriveSharedDrive.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
@ -2216,6 +2224,7 @@ private void InitializeComponent()
//
// tpGoogleCloudStorage
//
this.tpGoogleCloudStorage.Controls.Add(this.oauth2GoogleCloudStorage);
this.tpGoogleCloudStorage.Controls.Add(this.gbGoogleCloudStorageAdvanced);
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStoragePathPreview);
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStoragePathPreviewLabel);
@ -2225,7 +2234,6 @@ private void InitializeComponent()
this.tpGoogleCloudStorage.Controls.Add(this.txtGoogleCloudStorageDomain);
this.tpGoogleCloudStorage.Controls.Add(this.lblGoogleCloudStorageBucket);
this.tpGoogleCloudStorage.Controls.Add(this.txtGoogleCloudStorageBucket);
this.tpGoogleCloudStorage.Controls.Add(this.oauth2GoogleCloudStorage);
resources.ApplyResources(this.tpGoogleCloudStorage, "tpGoogleCloudStorage");
this.tpGoogleCloudStorage.Name = "tpGoogleCloudStorage";
this.tpGoogleCloudStorage.UseVisualStyleBackColor = true;
@ -2317,16 +2325,6 @@ private void InitializeComponent()
this.txtGoogleCloudStorageBucket.Name = "txtGoogleCloudStorageBucket";
this.txtGoogleCloudStorageBucket.TextChanged += new System.EventHandler(this.txtGoogleCloudStorageBucket_TextChanged);
//
// oauth2GoogleCloudStorage
//
resources.ApplyResources(this.oauth2GoogleCloudStorage, "oauth2GoogleCloudStorage");
this.oauth2GoogleCloudStorage.Name = "oauth2GoogleCloudStorage";
this.oauth2GoogleCloudStorage.UserInfo = null;
this.oauth2GoogleCloudStorage.OpenButtonClicked += new ShareX.UploadersLib.OAuthControl.OpenButtonClickedEventHandler(this.oauth2GoogleCloudStorage_OpenButtonClicked);
this.oauth2GoogleCloudStorage.CompleteButtonClicked += new ShareX.UploadersLib.OAuthControl.CompleteButtonClickedEventHandler(this.oauth2GoogleCloudStorage_CompleteButtonClicked);
this.oauth2GoogleCloudStorage.ClearButtonClicked += new ShareX.UploadersLib.OAuthControl.ClearButtonclickedEventHandler(this.oauth2GoogleCloudStorage_ClearButtonClicked);
this.oauth2GoogleCloudStorage.RefreshButtonClicked += new ShareX.UploadersLib.OAuthControl.RefreshButtonClickedEventHandler(this.oauth2GoogleCloudStorage_RefreshButtonClicked);
//
// tpAzureStorage
//
this.tpAzureStorage.BackColor = System.Drawing.SystemColors.Window;
@ -4945,13 +4943,13 @@ private void InitializeComponent()
this.actRapidShareAccountType.Name = "actRapidShareAccountType";
this.actRapidShareAccountType.SelectedAccountType = ShareX.UploadersLib.AccountType.Anonymous;
//
// oauth2GoogleDrive
// oauth2GoogleCloudStorage
//
resources.ApplyResources(this.oauth2GoogleDrive, "oauth2GoogleDrive");
this.oauth2GoogleDrive.Name = "oauth2GoogleDrive";
this.oauth2GoogleDrive.UserInfo = null;
this.oauth2GoogleDrive.ConnectButtonClicked += new System.Action(this.oauth2GoogleDrive_ConnectButtonClicked);
this.oauth2GoogleDrive.DisconnectButtonClicked += new System.Action(this.oauth2GoogleDrive_DisconnectButtonClicked);
resources.ApplyResources(this.oauth2GoogleCloudStorage, "oauth2GoogleCloudStorage");
this.oauth2GoogleCloudStorage.Name = "oauth2GoogleCloudStorage";
this.oauth2GoogleCloudStorage.UserInfo = null;
this.oauth2GoogleCloudStorage.ConnectButtonClicked += new System.Action(this.oauth2GoogleCloudStorage_ConnectButtonClicked);
this.oauth2GoogleCloudStorage.DisconnectButtonClicked += new System.Action(this.oauth2GoogleCloudStorage_DisconnectButtonClicked);
//
// UploadersConfigForm
//
@ -5604,7 +5602,6 @@ private void InitializeComponent()
internal System.Windows.Forms.TabPage tpYouTube;
private System.Windows.Forms.CheckBox cbYouTubeUseShortenedLink;
internal System.Windows.Forms.TabPage tpGoogleCloudStorage;
private OAuthControl oauth2GoogleCloudStorage;
private System.Windows.Forms.TextBox txtGoogleCloudStorageBucket;
private System.Windows.Forms.Label lblGoogleCloudStorageBucket;
private System.Windows.Forms.TextBox txtGoogleCloudStorageDomain;
@ -5694,5 +5691,6 @@ private void InitializeComponent()
private OAuthLoopbackControl oauth2YouTube;
private OAuthLoopbackControl oauth2GooglePhotos;
private OAuthLoopbackControl oauth2GoogleDrive;
private OAuthLoopbackControl oauth2GoogleCloudStorage;
}
}

View file

@ -752,7 +752,8 @@ private void LoadFileUploaderSettings()
if (OAuth2Info.CheckOAuth(Config.GoogleCloudStorageOAuth2Info))
{
oauth2GoogleCloudStorage.Status = OAuthLoginStatus.LoginSuccessful;
oauth2GoogleCloudStorage.Connected = true;
oauth2GoogleCloudStorage.UserInfo = Config.GoogleCloudStorageUserInfo;
}
txtGoogleCloudStorageBucket.Text = Config.GoogleCloudStorageBucket;
@ -3081,25 +3082,27 @@ private void cbYouTubeShowDialog_CheckedChanged(object sender, EventArgs e)
#region Google Cloud Storage
private void oauth2GoogleCloudStorage_ClearButtonClicked()
{
Config.GoogleCloudStorageOAuth2Info = null;
}
private void oauth2GoogleCloudStorage_CompleteButtonClicked(string code)
{
OAuth2Complete(new GoogleCloudStorage(Config.GoogleCloudStorageOAuth2Info), code, oauth2GoogleCloudStorage);
}
private void oauth2GoogleCloudStorage_OpenButtonClicked()
private void oauth2GoogleCloudStorage_ConnectButtonClicked()
{
OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret);
Config.GoogleCloudStorageOAuth2Info = OAuth2Open(new GoogleCloudStorage(oauth));
IOAuth2Loopback oauthLoopback = new GoogleCloudStorage(oauth).OAuth2;
using (OAuthListenerForm form = new OAuthListenerForm(oauthLoopback))
{
form.ShowDialog();
Config.GoogleCloudStorageOAuth2Info = form.OAuth2Info;
Config.GoogleCloudStorageUserInfo = form.UserInfo;
}
oauth2GoogleCloudStorage.Connected = OAuth2Info.CheckOAuth(Config.GoogleCloudStorageOAuth2Info);
oauth2GoogleCloudStorage.UserInfo = Config.GoogleCloudStorageUserInfo;
this.ForceActivate();
}
private void oauth2GoogleCloudStorage_RefreshButtonClicked()
private void oauth2GoogleCloudStorage_DisconnectButtonClicked()
{
OAuth2Refresh(new GoogleCloudStorage(Config.GoogleCloudStorageOAuth2Info), oauth2GoogleCloudStorage);
Config.GoogleCloudStorageOAuth2Info = null;
Config.GoogleCloudStorageUserInfo = null;
}
private void txtGoogleCloudStorageBucket_TextChanged(object sender, EventArgs e)

File diff suppressed because it is too large Load diff

View file

@ -432,6 +432,7 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
#region Google Cloud Storage
public OAuth2Info GoogleCloudStorageOAuth2Info { get; set; } = null;
public OAuthUserInfo GoogleCloudStorageUserInfo { get; set; } = null;
public string GoogleCloudStorageBucket { get; set; } = "";
public string GoogleCloudStorageDomain { get; set; } = "";
public string GoogleCloudStorageObjectPrefix { get; set; } = "ShareX/%y/%mo";