mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
OneDrive authentication works
This commit is contained in:
parent
b6e9543f42
commit
470818f753
7 changed files with 1163 additions and 4724 deletions
|
@ -45,8 +45,13 @@ public GoogleDrive(OAuth2Info oauth)
|
|||
|
||||
public string GetAuthorizationURL()
|
||||
{
|
||||
return string.Format("https://accounts.google.com/o/oauth2/auth?response_type={0}&client_id={1}&redirect_uri={2}&scope={3}",
|
||||
"code", AuthInfo.Client_ID, "urn:ietf:wg:oauth:2.0:oob", URLHelpers.URLEncode("https://www.googleapis.com/auth/drive"));
|
||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||
args.Add("response_type", "code");
|
||||
args.Add("client_id", AuthInfo.Client_ID);
|
||||
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
|
||||
args.Add("scope", "https://www.googleapis.com/auth/drive");
|
||||
|
||||
return CreateQuery("https://accounts.google.com/o/oauth2/auth", args);
|
||||
}
|
||||
|
||||
public bool GetAccessToken(string code)
|
||||
|
@ -105,13 +110,6 @@ public bool RefreshAccessToken()
|
|||
return false;
|
||||
}
|
||||
|
||||
private NameValueCollection GetAuthHeaders()
|
||||
{
|
||||
NameValueCollection headers = new NameValueCollection();
|
||||
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
|
||||
return headers;
|
||||
}
|
||||
|
||||
public bool CheckAuthorization()
|
||||
{
|
||||
if (OAuth2Info.CheckOAuth(AuthInfo))
|
||||
|
@ -131,6 +129,13 @@ public bool CheckAuthorization()
|
|||
return true;
|
||||
}
|
||||
|
||||
private NameValueCollection GetAuthHeaders()
|
||||
{
|
||||
NameValueCollection headers = new NameValueCollection();
|
||||
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
|
||||
return headers;
|
||||
}
|
||||
|
||||
private void SetMetadata(string fileID, string title, string parentID = null)
|
||||
{
|
||||
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}", fileID);
|
||||
|
|
139
ShareX.UploadersLib/FileUploaders/OneDrive.cs
Normal file
139
ShareX.UploadersLib/FileUploaders/OneDrive.cs
Normal file
|
@ -0,0 +1,139 @@
|
|||
#region License Information (GPL v3)
|
||||
|
||||
/*
|
||||
ShareX - A program that allows you to take screenshots and share any file type
|
||||
Copyright (C) 2007-2014 ShareX Developers
|
||||
|
||||
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)
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using ShareX.HelpersLib;
|
||||
using ShareX.UploadersLib.HelperClasses;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace ShareX.UploadersLib.FileUploaders
|
||||
{
|
||||
public sealed class OneDrive : FileUploader, IOAuth2
|
||||
{
|
||||
public OAuth2Info AuthInfo { get; set; }
|
||||
public string FolderID { get; set; }
|
||||
|
||||
public OneDrive(OAuth2Info authInfo)
|
||||
{
|
||||
AuthInfo = authInfo;
|
||||
}
|
||||
|
||||
public string GetAuthorizationURL()
|
||||
{
|
||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||
args.Add("client_id", AuthInfo.Client_ID);
|
||||
args.Add("scope", "wl.offline_access wl.basic wl.skydrive_update");
|
||||
args.Add("response_type", "code");
|
||||
args.Add("redirect_uri", Links.URL_CALLBACK);
|
||||
|
||||
return CreateQuery("https://login.live.com/oauth20_authorize.srf", args);
|
||||
}
|
||||
|
||||
public bool GetAccessToken(string code)
|
||||
{
|
||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||
args.Add("client_id", AuthInfo.Client_ID);
|
||||
args.Add("redirect_uri", Links.URL_CALLBACK);
|
||||
args.Add("client_secret", AuthInfo.Client_Secret);
|
||||
args.Add("code", code);
|
||||
args.Add("grant_type", "authorization_code");
|
||||
|
||||
string response = SendRequestURLEncoded("https://login.live.com/oauth20_token.srf", args);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
||||
|
||||
if (token != null && !string.IsNullOrEmpty(token.access_token))
|
||||
{
|
||||
token.UpdateExpireDate();
|
||||
AuthInfo.Token = token;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool RefreshAccessToken()
|
||||
{
|
||||
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
|
||||
{
|
||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||
args.Add("client_id", AuthInfo.Client_ID);
|
||||
args.Add("redirect_uri", Links.URL_CALLBACK);
|
||||
args.Add("client_secret", AuthInfo.Client_Secret);
|
||||
args.Add("refresh_token", AuthInfo.Token.refresh_token);
|
||||
args.Add("grant_type", "refresh_token");
|
||||
|
||||
string response = SendRequestURLEncoded("https://login.live.com/oauth20_token.srf", args);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
|
||||
|
||||
if (token != null && !string.IsNullOrEmpty(token.access_token))
|
||||
{
|
||||
token.UpdateExpireDate();
|
||||
string refresh_token = AuthInfo.Token.refresh_token;
|
||||
AuthInfo.Token = token;
|
||||
AuthInfo.Token.refresh_token = refresh_token;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool CheckAuthorization()
|
||||
{
|
||||
if (OAuth2Info.CheckOAuth(AuthInfo))
|
||||
{
|
||||
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
|
||||
{
|
||||
Errors.Add("Refresh access token failed.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Errors.Add("Login is required.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override UploadResult Upload(Stream stream, string fileName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1547,12 +1547,12 @@ private void InitializeComponent()
|
|||
//
|
||||
// oAuth2OneDrive
|
||||
//
|
||||
this.oAuth2OneDrive.IsRefreshable = false;
|
||||
resources.ApplyResources(this.oAuth2OneDrive, "oAuth2OneDrive");
|
||||
this.oAuth2OneDrive.Name = "oAuth2OneDrive";
|
||||
this.oAuth2OneDrive.OpenButtonClicked += new ShareX.UploadersLib.OAuthControl.OpenButtonClickedEventHandler(this.oAuth2OneDrive_OpenButtonClicked);
|
||||
this.oAuth2OneDrive.CompleteButtonClicked += new ShareX.UploadersLib.OAuthControl.CompleteButtonClickedEventHandler(this.oAuth2OneDrive_CompleteButtonClicked);
|
||||
this.oAuth2OneDrive.ClearButtonClicked += new ShareX.UploadersLib.OAuthControl.ClearButtonclickedEventHandler(this.oAuth2OneDrive_ClearButtonClicked);
|
||||
this.oAuth2OneDrive.RefreshButtonClicked += new ShareX.UploadersLib.OAuthControl.RefreshButtonClickedEventHandler(this.oAuth2OneDrive_RefreshButtonClicked);
|
||||
//
|
||||
// tpAmazonS3
|
||||
//
|
||||
|
|
|
@ -50,7 +50,7 @@ public UploadersConfigForm(UploadersConfig uploadersConfig)
|
|||
InitializeComponent();
|
||||
|
||||
// TODO: Add OneDrive support
|
||||
tcFileUploaders.TabPages.Remove(tpOneDrive);
|
||||
//tcFileUploaders.TabPages.Remove(tpOneDrive);
|
||||
|
||||
if (!string.IsNullOrEmpty(Config.FilePath))
|
||||
{
|
||||
|
@ -332,6 +332,13 @@ public void LoadSettings(UploadersConfig uploadersConfig)
|
|||
txtGoogleDriveFolderID.Enabled = Config.GoogleDriveUseFolder;
|
||||
txtGoogleDriveFolderID.Text = Config.GoogleDriveFolderID;
|
||||
|
||||
// One Drive
|
||||
|
||||
if (OAuth2Info.CheckOAuth(Config.OneDriveOAuth2Info))
|
||||
{
|
||||
oAuth2OneDrive.Status = OAuthLoginStatus.LoginSuccessful;
|
||||
}
|
||||
|
||||
// Minus
|
||||
|
||||
cbMinusURLType.Items.Clear();
|
||||
|
@ -1774,6 +1781,11 @@ private void oAuth2OneDrive_CompleteButtonClicked(string code)
|
|||
OneDriveAuthComplete(code);
|
||||
}
|
||||
|
||||
private void oAuth2OneDrive_RefreshButtonClicked()
|
||||
{
|
||||
OneDriveAuthRefresh();
|
||||
}
|
||||
|
||||
private void oAuth2OneDrive_ClearButtonClicked()
|
||||
{
|
||||
Config.OneDriveOAuth2Info = null;
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -898,7 +898,7 @@ private void BoxAddFolder(BoxFileEntry folder)
|
|||
|
||||
public void OneDriveAuthOpen()
|
||||
{
|
||||
/*try
|
||||
try
|
||||
{
|
||||
OAuth2Info oauth = new OAuth2Info(APIKeys.OneDriveClientID, APIKeys.OneDriveClientSecret);
|
||||
string url = new OneDrive(oauth).GetAuthorizationURL();
|
||||
|
@ -917,12 +917,12 @@ public void OneDriveAuthOpen()
|
|||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.ToString(), "ShareX - " + Resources.UploadersConfigForm_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
public void OneDriveAuthComplete(string code)
|
||||
{
|
||||
/*try
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(code) && Config.OneDriveOAuth2Info != null)
|
||||
{
|
||||
|
@ -946,7 +946,33 @@ public void OneDriveAuthComplete(string code)
|
|||
{
|
||||
DebugHelper.WriteException(ex);
|
||||
MessageBox.Show(ex.ToString(), "ShareX - " + Resources.UploadersConfigForm_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
public void OneDriveAuthRefresh()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (OAuth2Info.CheckOAuth(Config.OneDriveOAuth2Info))
|
||||
{
|
||||
bool result = new OneDrive(Config.OneDriveOAuth2Info).RefreshAccessToken();
|
||||
|
||||
if (result)
|
||||
{
|
||||
oAuth2OneDrive.Status = OAuthLoginStatus.LoginSuccessful;
|
||||
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
oAuth2OneDrive.Status = OAuthLoginStatus.LoginFailed;
|
||||
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.ToString(), Resources.UploadersConfigForm_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion OneDrive
|
||||
|
|
|
@ -109,6 +109,7 @@
|
|||
<Compile Include="FileUploaders\Hostr.cs" />
|
||||
<Compile Include="FileUploaders\MediaFire.cs" />
|
||||
<Compile Include="FileUploaders\Mega.cs" />
|
||||
<Compile Include="FileUploaders\OneDrive.cs" />
|
||||
<Compile Include="FileUploaders\OwnCloud.cs" />
|
||||
<Compile Include="FileUploaders\Pomf.cs" />
|
||||
<Compile Include="FileUploaders\SFTP.cs" />
|
||||
|
|
Loading…
Reference in a new issue