Dropbox OAuth2 implementation interim commit

This commit is contained in:
Jaex 2014-06-08 22:11:44 +03:00
parent 7cbc98accb
commit 1d8caac431
16 changed files with 212 additions and 155 deletions

View file

@ -805,7 +805,7 @@ public UploadResult UploadFile(Stream stream, string fileName)
switch (fileDestination)
{
case FileDestination.Dropbox:
fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuthInfo, Program.UploadersConfig.DropboxAccountInfo)
fileUploader = new Dropbox(Program.UploadersConfig.DropboxOAuth2Info, Program.UploadersConfig.DropboxAccountInfo)
{
UploadPath = NameParser.Parse(NameParserType.URL, Dropbox.TidyUploadPath(Program.UploadersConfig.DropboxUploadPath)),
AutoCreateShareableLink = Program.UploadersConfig.DropboxAutoCreateShareableLink,

View file

@ -113,6 +113,13 @@ 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))
@ -146,10 +153,7 @@ public BoxFileInfo GetFiles(string id)
string url = string.Format("https://api.box.com/2.0/folders/{0}/items", id);
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
string response = SendRequest(HttpMethod.GET, url, headers: headers);
string response = SendRequest(HttpMethod.GET, url, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -161,10 +165,7 @@ public BoxFileInfo GetFiles(string id)
public string CreateSharedLink(string id)
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
string response = SendRequest(HttpMethod.PUT, "https://api.box.com/2.0/files/" + id, "{\"shared_link\": {\"access\": \"open\"}}", headers: headers);
string response = SendRequest(HttpMethod.PUT, "https://api.box.com/2.0/files/" + id, "{\"shared_link\": {\"access\": \"open\"}}", headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -194,10 +195,7 @@ public override UploadResult Upload(Stream stream, string fileName)
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("parent_id", FolderID);
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
UploadResult result = UploadData(stream, "https://upload.box.com/api/2.0/files/content", fileName, "filename", args, headers: headers);
UploadResult result = UploadData(stream, "https://upload.box.com/api/2.0/files/content", fileName, "filename", args, headers: GetAuthHeaders());
if (result.IsSuccess)
{

View file

@ -27,15 +27,16 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
using UploadersLib.HelperClasses;
namespace UploadersLib.FileUploaders
{
public sealed class Dropbox : FileUploader, IOAuth
public sealed class Dropbox : FileUploader, IOAuth2Basic
{
public OAuthInfo AuthInfo { get; set; }
public OAuth2Info AuthInfo { get; set; }
public DropboxAccountInfo AccountInfo { get; set; }
public string UploadPath { get; set; }
public bool AutoCreateShareableLink { get; set; }
@ -44,6 +45,7 @@ public sealed class Dropbox : FileUploader, IOAuth
private const string APIVersion = "1";
private const string Root = "dropbox"; // dropbox or sandbox
private const string URLWEB = "https://www.dropbox.com/" + APIVersion;
private const string URLAPI = "https://api.dropbox.com/" + APIVersion;
private const string URLAPIContent = "https://api-content.dropbox.com/" + APIVersion;
@ -58,47 +60,85 @@ public sealed class Dropbox : FileUploader, IOAuth
private const string URLPublicDirect = "https://dl.dropboxusercontent.com/u";
private const string URLShareDirect = "https://dl.dropboxusercontent.com/s";
private const string URLRequestToken = URLAPI + "/oauth/request_token";
private const string URLAuthorize = "https://www.dropbox.com/" + APIVersion + "/oauth/authorize";
private const string URLAccessToken = URLAPI + "/oauth/access_token";
public Dropbox(OAuthInfo oauth)
public Dropbox(OAuth2Info oauth)
{
AuthInfo = oauth;
}
public Dropbox(OAuthInfo oauth, DropboxAccountInfo accountInfo)
public Dropbox(OAuth2Info oauth, DropboxAccountInfo accountInfo)
: this(oauth)
{
AccountInfo = accountInfo;
}
// https://www.dropbox.com/developers/core/api#request-token
// https://www.dropbox.com/developers/core/api#authorize
// https://www.dropbox.com/developers/core/docs#oa2-authorize
public string GetAuthorizationURL()
{
return GetAuthorizationURL(URLRequestToken, URLAuthorize, AuthInfo);
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("response_type", "code");
args.Add("client_id", AuthInfo.Client_ID);
return CreateQuery(URLWEB + "/oauth2/authorize", args);
}
// https://www.dropbox.com/developers/core/api#access-token
// https://www.dropbox.com/developers/core/docs#oa2-token
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "authorization_code");
args.Add("code", code);
string response = SendRequest(HttpMethod.POST, URLAPI + "/oauth2/token", args);
if (!string.IsNullOrEmpty(response))
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
AuthInfo.Token = token;
return true;
}
}
return false;
}
private NameValueCollection GetAuthHeaders()
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
return headers;
}
/* OAuth 1.0
// https://www.dropbox.com/developers/core/docs#request-token
// https://www.dropbox.com/developers/core/docs#authorize
public string GetAuthorizationURL()
{
return GetAuthorizationURL(URLAPI + "/oauth/request_token", URLWEB + "/oauth/authorize", AuthInfo);
}
// https://www.dropbox.com/developers/core/docs#access-token
public bool GetAccessToken(string verificationCode = null)
{
AuthInfo.AuthVerifier = verificationCode;
return GetAccessToken(URLAccessToken, AuthInfo);
return GetAccessToken(URLAPI + "/oauth/access_token", AuthInfo);
}
*/
#region Dropbox accounts
// https://www.dropbox.com/developers/core/api#account-info
// https://www.dropbox.com/developers/core/docs#account-info
public DropboxAccountInfo GetAccountInfo()
{
DropboxAccountInfo account = null;
if (OAuthInfo.CheckOAuth(AuthInfo))
if (OAuth2Info.CheckOAuth(AuthInfo))
{
string query = OAuthManager.GenerateQuery(URLAccountInfo, null, HttpMethod.GET, AuthInfo);
string response = SendRequest(HttpMethod.GET, query);
string response = SendRequest(HttpMethod.GET, URLAccountInfo, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -118,23 +158,22 @@ public DropboxAccountInfo GetAccountInfo()
#region Files and metadata
// https://www.dropbox.com/developers/core/api#files-GET
// https://www.dropbox.com/developers/core/docs#files-GET
public bool DownloadFile(string path, Stream downloadStream)
{
if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo))
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{
string url = Helpers.CombineURL(URLFiles, Helpers.URLPathEncode(path));
string query = OAuthManager.GenerateQuery(url, null, HttpMethod.GET, AuthInfo);
return SendRequest(HttpMethod.GET, downloadStream, query);
return SendRequest(HttpMethod.GET, downloadStream, url, headers: GetAuthHeaders());
}
return false;
}
// https://www.dropbox.com/developers/core/api#files-POST
// https://www.dropbox.com/developers/core/docs#files_put
public UploadResult UploadFile(Stream stream, string path, string fileName, bool createShareableURL = false, DropboxURLType urlType = DropboxURLType.Default)
{
if (!OAuthInfo.CheckOAuth(AuthInfo))
if (!OAuth2Info.CheckOAuth(AuthInfo))
{
Errors.Add("Dropbox login is required.");
return null;
@ -145,10 +184,8 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("file", fileName);
string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo);
// There's a 150MB limit to all uploads through the API.
UploadResult result = UploadData(stream, query, fileName);
UploadResult result = UploadData(stream, url, fileName, arguments: args, headers: GetAuthHeaders());
if (result.IsSuccess)
{
@ -170,21 +207,19 @@ public UploadResult UploadFile(Stream stream, string path, string fileName, bool
return result;
}
// https://www.dropbox.com/developers/core/api#metadata
// https://www.dropbox.com/developers/core/docs#metadata
public DropboxContentInfo GetMetadata(string path, bool list)
{
DropboxContentInfo contentInfo = null;
if (OAuthInfo.CheckOAuth(AuthInfo))
if (OAuth2Info.CheckOAuth(AuthInfo))
{
string url = Helpers.CombineURL(URLMetaData, Helpers.URLPathEncode(path));
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("list", list ? "true" : "false");
string query = OAuthManager.GenerateQuery(url, args, HttpMethod.GET, AuthInfo);
string response = SendRequest(HttpMethod.GET, query);
string response = SendRequest(HttpMethod.GET, url, args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -201,19 +236,17 @@ public bool IsExists(string path)
return contentInfo != null && !contentInfo.Is_deleted;
}
// https://www.dropbox.com/developers/core/api#shares
// https://www.dropbox.com/developers/core/docs#shares
public string CreateShareableLink(string path, DropboxURLType urlType)
{
if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo))
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{
string url = Helpers.CombineURL(URLShares, Helpers.URLPathEncode(path));
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("short_url", urlType == DropboxURLType.Shortened ? "true" : "false");
string query = OAuthManager.GenerateQuery(url, args, HttpMethod.POST, AuthInfo);
string response = SendRequest(HttpMethod.POST, query);
string response = SendRequest(HttpMethod.POST, url, args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -245,21 +278,19 @@ public string CreateShareableLink(string path, DropboxURLType urlType)
#region File operations
// https://www.dropbox.com/developers/core/api#fileops-copy
// https://www.dropbox.com/developers/core/docs#fileops-copy
public DropboxContentInfo Copy(string from_path, string to_path)
{
DropboxContentInfo contentInfo = null;
if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuthInfo.CheckOAuth(AuthInfo))
if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("root", Root);
args.Add("from_path", from_path);
args.Add("to_path", to_path);
string query = OAuthManager.GenerateQuery(URLCopy, args, HttpMethod.POST, AuthInfo);
string response = SendRequest(HttpMethod.POST, query);
string response = SendRequest(HttpMethod.POST, URLCopy, args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -270,20 +301,18 @@ public DropboxContentInfo Copy(string from_path, string to_path)
return contentInfo;
}
// https://www.dropbox.com/developers/core/api#fileops-create-folder
// https://www.dropbox.com/developers/core/docs#fileops-create-folder
public DropboxContentInfo CreateFolder(string path)
{
DropboxContentInfo contentInfo = null;
if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo))
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("root", Root);
args.Add("path", path);
string query = OAuthManager.GenerateQuery(URLCreateFolder, args, HttpMethod.POST, AuthInfo);
string response = SendRequest(HttpMethod.POST, query);
string response = SendRequest(HttpMethod.POST, URLCreateFolder, args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -294,20 +323,18 @@ public DropboxContentInfo CreateFolder(string path)
return contentInfo;
}
// https://www.dropbox.com/developers/core/api#fileops-delete
// https://www.dropbox.com/developers/core/docs#fileops-delete
public DropboxContentInfo Delete(string path)
{
DropboxContentInfo contentInfo = null;
if (!string.IsNullOrEmpty(path) && OAuthInfo.CheckOAuth(AuthInfo))
if (!string.IsNullOrEmpty(path) && OAuth2Info.CheckOAuth(AuthInfo))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("root", Root);
args.Add("path", path);
string query = OAuthManager.GenerateQuery(URLDelete, args, HttpMethod.POST, AuthInfo);
string response = SendRequest(HttpMethod.POST, query);
string response = SendRequest(HttpMethod.POST, URLDelete, args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -318,21 +345,19 @@ public DropboxContentInfo Delete(string path)
return contentInfo;
}
// https://www.dropbox.com/developers/core/api#fileops-move
// https://www.dropbox.com/developers/core/docs#fileops-move
public DropboxContentInfo Move(string from_path, string to_path)
{
DropboxContentInfo contentInfo = null;
if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuthInfo.CheckOAuth(AuthInfo))
if (!string.IsNullOrEmpty(from_path) && !string.IsNullOrEmpty(to_path) && OAuth2Info.CheckOAuth(AuthInfo))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("root", Root);
args.Add("from_path", from_path);
args.Add("to_path", to_path);
string query = OAuthManager.GenerateQuery(URLMove, args, HttpMethod.POST, AuthInfo);
string response = SendRequest(HttpMethod.POST, query);
string response = SendRequest(HttpMethod.POST, URLMove, args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{

View file

@ -104,6 +104,13 @@ 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))
@ -125,9 +132,6 @@ public bool CheckAuthorization()
public void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, string value, bool withLink)
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions", fileID);
string json = JsonConvert.SerializeObject(new
@ -138,7 +142,7 @@ public void SetPermissions(string fileID, GoogleDrivePermissionRole role, Google
withLink = withLink.ToString()
});
string response = SendPostRequestJSON(url, json, headers: headers);
string response = SendPostRequestJSON(url, json, headers: GetAuthHeaders());
}
public override UploadResult Upload(Stream stream, string fileName)
@ -148,13 +152,10 @@ public override UploadResult Upload(Stream stream, string fileName)
return null;
}
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("title", fileName);
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, "file", args, headers: headers);
UploadResult result = UploadData(stream, "https://www.googleapis.com/upload/drive/v2/files", fileName, "file", args, headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(result.Response))
{

View file

@ -44,7 +44,7 @@ public partial class DropboxFilesForm : Form
private ImageListManager ilm;
private bool isSelectedFile, isSelectedPublic;
public DropboxFilesForm(OAuthInfo oauth, string path, DropboxAccountInfo accountInfo)
public DropboxFilesForm(OAuth2Info oauth, string path, DropboxAccountInfo accountInfo)
{
InitializeComponent();
Icon = Resources.Dropbox;

View file

@ -436,9 +436,9 @@ public void PicasaRefreshAlbumList()
public void DropboxOpenFiles()
{
if (OAuthInfo.CheckOAuth(Config.DropboxOAuthInfo))
if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info))
{
using (DropboxFilesForm filesForm = new DropboxFilesForm(Config.DropboxOAuthInfo, GetDropboxUploadPath(), Config.DropboxAccountInfo))
using (DropboxFilesForm filesForm = new DropboxFilesForm(Config.DropboxOAuth2Info, GetDropboxUploadPath(), Config.DropboxAccountInfo))
{
if (filesForm.ShowDialog() == DialogResult.OK)
{
@ -452,13 +452,13 @@ public void DropboxAuthOpen()
{
try
{
OAuthInfo oauth = new OAuthInfo(APIKeys.DropboxConsumerKey, APIKeys.DropboxConsumerSecret);
OAuth2Info oauth = new OAuth2Info(APIKeys.DropboxConsumerKey, APIKeys.DropboxConsumerSecret);
string url = new Dropbox(oauth).GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
Config.DropboxOAuthInfo = oauth;
Config.DropboxOAuth2Info = oauth;
Helpers.OpenURL(url);
btnDropboxCompleteAuth.Enabled = true;
DebugHelper.WriteLine("DropboxAuthOpen - Authorization URL is opened: " + url);
@ -476,51 +476,51 @@ public void DropboxAuthOpen()
public void DropboxAuthComplete()
{
try
{
if (Config.DropboxOAuthInfo != null && !string.IsNullOrEmpty(Config.DropboxOAuthInfo.AuthToken) && !string.IsNullOrEmpty(Config.DropboxOAuthInfo.AuthSecret))
{
Dropbox dropbox = new Dropbox(Config.DropboxOAuthInfo);
bool result = dropbox.GetAccessToken();
/* try
{
if (Config.DropboxOAuth2Info != null && !string.IsNullOrEmpty(Config.DropboxOAuth2Info.AuthToken) && !string.IsNullOrEmpty(Config.DropboxOAuth2Info.AuthSecret))
{
Dropbox dropbox = new Dropbox(Config.DropboxOAuth2Info);
bool result = dropbox.GetAccessToken();
if (result)
{
DropboxAccountInfo account = dropbox.GetAccountInfo();
if (result)
{
DropboxAccountInfo account = dropbox.GetAccountInfo();
if (account != null)
{
Config.DropboxAccountInfo = account;
Config.DropboxUploadPath = txtDropboxPath.Text;
UpdateDropboxStatus();
MessageBox.Show("Login successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (account != null)
{
Config.DropboxAccountInfo = account;
Config.DropboxUploadPath = txtDropboxPath.Text;
UpdateDropboxStatus();
MessageBox.Show("Login successful.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MessageBox.Show("GetAccountInfo failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You must give access from Authorize page first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show("GetAccountInfo failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You must give access from Authorize page first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Config.DropboxOAuthInfo = null;
UpdateDropboxStatus();
}
catch (Exception ex)
{
DebugHelper.WriteException(ex);
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Config.DropboxOAut2hInfo = null;
UpdateDropboxStatus();
}
catch (Exception ex)
{
DebugHelper.WriteException(ex);
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} */
}
private void UpdateDropboxStatus()
{
if (OAuthInfo.CheckOAuth(Config.DropboxOAuthInfo) && Config.DropboxAccountInfo != null)
if (OAuth2Info.CheckOAuth(Config.DropboxOAuth2Info) && Config.DropboxAccountInfo != null)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Login status: Successful");

View file

@ -25,12 +25,8 @@
namespace UploadersLib.HelperClasses
{
public interface IOAuth
public interface IOAuth : IOAuthBase
{
OAuthInfo AuthInfo { get; set; }
string GetAuthorizationURL();
bool GetAccessToken(string verificationCode);
OAuthInfo AuthInfo { get; }
}
}

View file

@ -25,14 +25,8 @@
namespace UploadersLib.HelperClasses
{
public interface IOAuth2
public interface IOAuth2 : IOAuth2Basic
{
OAuth2Info AuthInfo { get; set; }
string GetAuthorizationURL();
bool GetAccessToken(string pin);
bool RefreshAccessToken();
bool CheckAuthorization();

View file

@ -0,0 +1,34 @@
#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 System.Collections.Specialized;
namespace UploadersLib.HelperClasses
{
public interface IOAuth2Basic : IOAuthBase
{
OAuth2Info AuthInfo { get; }
}
}

View file

@ -25,10 +25,8 @@
namespace UploadersLib.HelperClasses
{
public interface IOAuth2Simple
public interface IOAuthBase
{
OAuth2Info AuthInfo { get; }
string GetAuthorizationURL();
bool GetAccessToken(string code);

View file

@ -122,6 +122,13 @@ 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))
@ -145,9 +152,7 @@ public List<ImgurAlbumData> GetAlbums()
{
if (CheckAuthorization())
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
string response = SendRequest(HttpMethod.GET, "https://api.imgur.com/3/account/me/albums", null, ResponseType.Text, headers, null);
string response = SendRequest(HttpMethod.GET, "https://api.imgur.com/3/account/me/albums", headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -171,7 +176,7 @@ public List<ImgurAlbumData> GetAlbums()
public override UploadResult Upload(Stream stream, string fileName)
{
Dictionary<string, string> args = new Dictionary<string, string>();
NameValueCollection headers = new NameValueCollection();
NameValueCollection headers;
if (UploadMethod == AccountType.User)
{
@ -185,14 +190,15 @@ public override UploadResult Upload(Stream stream, string fileName)
args.Add("album", UploadAlbumID);
}
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
headers = GetAuthHeaders();
}
else if (UploadMethod == AccountType.Anonymous)
else
{
headers = new NameValueCollection();
headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID);
}
UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, null, headers);
UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers: headers);
if (!string.IsNullOrEmpty(result.Response))
{

View file

@ -109,6 +109,13 @@ 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))
@ -134,10 +141,7 @@ public List<PicasaAlbumInfo> GetAlbumList()
List<PicasaAlbumInfo> albumList = new List<PicasaAlbumInfo>();
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
string response = SendRequest(HttpMethod.GET, "https://picasaweb.google.com/data/feed/api/user/default", null, ResponseType.Text, headers, null);
string response = SendRequest(HttpMethod.GET, "https://picasaweb.google.com/data/feed/api/user/default", headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -173,11 +177,10 @@ public override UploadResult Upload(Stream stream, string fileName)
string url = string.Format("https://picasaweb.google.com/data/feed/api/user/default/albumid/" + AlbumID);
string contentType = Helpers.GetMimeType(fileName);
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
NameValueCollection headers = GetAuthHeaders();
headers.Add("Slug", Helpers.URLEncode(fileName));
ur.Response = SendPostRequestStream(url, stream, contentType, null, headers);
ur.Response = SendPostRequestStream(url, stream, contentType, headers: headers);
XDocument xd = XDocument.Parse(ur.Response);

View file

@ -31,7 +31,7 @@
namespace UploadersLib.TextUploaders
{
public sealed class Gist : TextUploader, IOAuth2Simple
public sealed class Gist : TextUploader, IOAuth2Basic
{
private const string URLAPI = "https://api.github.com/";
private const string URLGists = URLAPI + "gists";

View file

@ -31,7 +31,7 @@
namespace UploadersLib.URLShorteners
{
public sealed class BitlyURLShortener : URLShortener, IOAuth2Simple
public sealed class BitlyURLShortener : URLShortener, IOAuth2Basic
{
private const string URLAPI = "https://api-ssl.bitly.com/";
private const string URLAccessToken = URLAPI + "oauth/access_token";

View file

@ -116,7 +116,7 @@ public class UploadersConfig : SettingsBase<UploadersConfig>
// Dropbox
public OAuthInfo DropboxOAuthInfo = null;
public OAuth2Info DropboxOAuth2Info = null;
public DropboxAccountInfo DropboxAccountInfo = null;
public string DropboxUploadPath = "Public/ShareX/%y/%mo";
public bool DropboxAutoCreateShareableLink = false;
@ -331,7 +331,7 @@ public bool IsActive(FileDestination destination)
switch (destination)
{
case FileDestination.Dropbox:
return OAuthInfo.CheckOAuth(DropboxOAuthInfo);
return OAuth2Info.CheckOAuth(DropboxOAuth2Info);
case FileDestination.Copy:
return OAuthInfo.CheckOAuth(CopyOAuthInfo);
case FileDestination.GoogleDrive:

View file

@ -176,6 +176,7 @@
</Compile>
<Compile Include="FileUploaders\Pushbullet.cs" />
<Compile Include="FileUploaders\GfycatUploader.cs" />
<Compile Include="HelperClasses\OAuth\IOAuthBase.cs" />
<Compile Include="TextUploaders\Upaste.cs" />
<Compile Include="UploadersConfig.cs" />
<Compile Include="GUI\UserPassBox.cs">
@ -221,7 +222,7 @@
<Compile Include="HelperClasses\JSONHelper.cs" />
<Compile Include="HelperClasses\OAuth\IOAuth.cs" />
<Compile Include="HelperClasses\OAuth\IOAuth2.cs" />
<Compile Include="HelperClasses\OAuth\IOAuth2Simple.cs" />
<Compile Include="HelperClasses\OAuth\IOAuth2Basic.cs" />
<Compile Include="HelperClasses\OAuth\OAuth2Info.cs" />
<Compile Include="HelperClasses\OAuth\OAuth2Token.cs" />
<Compile Include="HelperClasses\OAuth\OAuthManager.cs" />
@ -351,6 +352,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram1.cd" />
<None Include="Favicons\Yourls.ico" />
<EmbeddedResource Include="APIKeys\jira_sharex.pfx" />
<None Include="Favicons\Box.ico" />