Update Google authentication to latest URLs

This commit is contained in:
SupSuper 2018-04-11 05:28:01 +01:00
parent c44611e9fc
commit 9bb156ac6b
5 changed files with 80 additions and 263 deletions

View file

@ -24,7 +24,6 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System;
using System.Collections.Generic;
@ -72,107 +71,39 @@ public enum GoogleDrivePermissionType
public sealed class GoogleDrive : FileUploader, IOAuth2
{
public OAuth2Info AuthInfo { get; set; }
private GoogleOAuth2 GoogleAuth { get; set; }
public bool IsPublic { get; set; }
public bool DirectLink { get; set; }
public string FolderID { get; set; }
public GoogleDrive(OAuth2Info oauth)
{
AuthInfo = oauth;
}
public string GetAuthorizationURL()
{
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 URLHelpers.CreateQuery("https://accounts.google.com/o/oauth2/auth", args);
}
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("code", code);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
args.Add("grant_type", "authorization_code");
string response = SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", args);
if (!string.IsNullOrEmpty(response))
GoogleAuth = new GoogleOAuth2(oauth, this)
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
return false;
Scope = "https://www.googleapis.com/auth/drive"
};
}
public OAuth2Info AuthInfo => GoogleAuth.AuthInfo;
public bool RefreshAccessToken()
{
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("refresh_token", AuthInfo.Token.refresh_token);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "refresh_token");
string response = SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", 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;
return GoogleAuth.RefreshAccessToken();
}
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;
return GoogleAuth.CheckAuthorization();
}
private NameValueCollection GetAuthHeaders()
public string GetAuthorizationURL()
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
return headers;
return GoogleAuth.GetAuthorizationURL();
}
public bool GetAccessToken(string code)
{
return GoogleAuth.GetAccessToken(code);
}
private string GetMetadata(string title, string parentID)
@ -218,7 +149,7 @@ private void SetPermissions(string fileID, GoogleDrivePermissionRole role, Googl
withLink = withLink.ToString()
});
string response = SendRequest(HttpMethod.POST, url, json, ContentTypeJSON, null, GetAuthHeaders());
string response = SendRequest(HttpMethod.POST, url, json, ContentTypeJSON, null, GoogleAuth.GetAuthHeaders());
}
public List<GoogleDriveFile> GetFolders(bool trashed = false, bool writer = true)
@ -240,7 +171,7 @@ public List<GoogleDriveFile> GetFolders(bool trashed = false, bool writer = true
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("q", query);
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v2/files", args, GetAuthHeaders());
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v2/files", args, GoogleAuth.GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
@ -261,7 +192,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string metadata = GetMetadata(fileName, FolderID);
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", stream, fileName, headers: GetAuthHeaders(),
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", stream, fileName, headers: GoogleAuth.GetAuthHeaders(),
contentType: "multipart/related", metadata: metadata);
if (!string.IsNullOrEmpty(result.Response))

View file

@ -64,7 +64,7 @@ public YouTube(OAuth2Info oauth)
{
GoogleAuth = new GoogleOAuth2(oauth, this)
{
Scope = "youtube.upload"
Scope = "https://www.googleapis.com/auth/youtube.upload"
};
}
@ -139,6 +139,7 @@ public override UploadResult Upload(Stream stream, string fileName)
case YouTubeVideoStatus.UploadFailed:
Errors.Add("Upload failed: " + upload.status.failureReason);
break;
case YouTubeVideoStatus.UploadRejected:
Errors.Add("Upload rejected: " + upload.status.rejectionReason);
break;

View file

@ -32,6 +32,10 @@ namespace ShareX.UploadersLib
{
public class GoogleOAuth2 : IOAuth2
{
private const string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
private const string TokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
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 Scope { get; set; }
@ -47,10 +51,10 @@ public string GetAuthorizationURL()
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/" + Scope);
args.Add("redirect_uri", RedirectMethod);
args.Add("scope", Scope);
return URLHelpers.CreateQuery("https://accounts.google.com/o/oauth2/auth", args);
return URLHelpers.CreateQuery(AuthorizationEndpoint, args);
}
public bool GetAccessToken(string code)
@ -59,10 +63,10 @@ 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", "urn:ietf:wg:oauth:2.0:oob");
args.Add("redirect_uri", RedirectMethod);
args.Add("grant_type", "authorization_code");
string response = GoogleUploader.SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", args);
string response = GoogleUploader.SendRequestMultiPart(TokenEndpoint, args);
if (!string.IsNullOrEmpty(response))
{
@ -89,7 +93,7 @@ public bool RefreshAccessToken()
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "refresh_token");
string response = GoogleUploader.SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", args);
string response = GoogleUploader.SendRequestMultiPart(TokenEndpoint, args);
if (!string.IsNullOrEmpty(response))
{

View file

@ -23,7 +23,6 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System.Collections.Generic;
@ -59,7 +58,7 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public class GooglePhotos : ImageUploader, IOAuth2
{
public OAuth2Info AuthInfo { get; set; }
private GoogleOAuth2 GoogleAuth { get; set; }
public string AlbumID { get; set; }
private static readonly XNamespace AtomNS = "http://www.w3.org/2005/Atom";
@ -68,96 +67,39 @@ public class GooglePhotos : ImageUploader, IOAuth2
public GooglePhotos(OAuth2Info oauth)
{
AuthInfo = 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://picasaweb.google.com/data"));
}
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("code", code);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
args.Add("grant_type", "authorization_code");
string response = SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", args);
if (!string.IsNullOrEmpty(response))
GoogleAuth = new GoogleOAuth2(oauth, this)
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
return false;
Scope = "https://picasaweb.google.com/data"
};
}
public OAuth2Info AuthInfo => GoogleAuth.AuthInfo;
public bool RefreshAccessToken()
{
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("refresh_token", AuthInfo.Token.refresh_token);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "refresh_token");
string response = SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", 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;
}
private NameValueCollection GetAuthHeaders()
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
headers.Add("GData-Version", "3");
return headers;
return GoogleAuth.RefreshAccessToken();
}
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 GoogleAuth.CheckAuthorization();
}
return true;
public string GetAuthorizationURL()
{
return GoogleAuth.GetAuthorizationURL();
}
public bool GetAccessToken(string code)
{
return GoogleAuth.GetAccessToken(code);
}
private NameValueCollection GetAuthHeaders()
{
NameValueCollection headers = GoogleAuth.GetAuthHeaders();
headers.Add("GData-Version", "3");
return headers;
}
public List<GooglePhotosAlbumInfo> GetAlbumList()

View file

@ -24,9 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using ShareX.UploadersLib.Properties;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
@ -53,108 +51,48 @@ public override URLShortener CreateShortener(UploadersConfig config, TaskReferen
public class GoogleURLShortener : URLShortener, IOAuth2
{
private GoogleOAuth2 GoogleAuth { get; set; }
public AccountType UploadMethod { get; set; }
public string AnonymousKey { get; set; }
public OAuth2Info AuthInfo { get; set; }
public GoogleURLShortener(AccountType uploadMethod, string anonymousKey, OAuth2Info oauth)
{
UploadMethod = uploadMethod;
AnonymousKey = anonymousKey;
AuthInfo = oauth;
}
public GoogleURLShortener(string anonymousKey)
{
UploadMethod = AccountType.Anonymous;
AnonymousKey = anonymousKey;
}
public GoogleURLShortener(OAuth2Info oauth)
{
UploadMethod = AccountType.User;
AuthInfo = 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/urlshortener"));
}
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("code", code);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
args.Add("grant_type", "authorization_code");
string response = SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", args);
if (!string.IsNullOrEmpty(response))
GoogleAuth = new GoogleOAuth2(oauth, this)
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
return false;
Scope = "https://www.googleapis.com/auth/urlshortener"
};
}
public GoogleURLShortener(string anonymousKey) : this(AccountType.Anonymous, anonymousKey, null)
{
}
public GoogleURLShortener(OAuth2Info oauth) : this(AccountType.User, null, oauth)
{
}
public OAuth2Info AuthInfo => GoogleAuth.AuthInfo;
public bool RefreshAccessToken()
{
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("refresh_token", AuthInfo.Token.refresh_token);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "refresh_token");
string response = SendRequestMultiPart("https://accounts.google.com/o/oauth2/token", 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;
return GoogleAuth.RefreshAccessToken();
}
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 GoogleAuth.CheckAuthorization();
}
return true;
public string GetAuthorizationURL()
{
return GoogleAuth.GetAuthorizationURL();
}
public bool GetAccessToken(string code)
{
return GoogleAuth.GetAccessToken(code);
}
public override UploadResult ShortenURL(string url)
@ -171,6 +109,7 @@ public override UploadResult ShortenURL(string url)
case AccountType.Anonymous:
query = string.Format("https://www.googleapis.com/urlshortener/v1/url?key={0}", AnonymousKey);
break;
case AccountType.User:
if (!CheckAuthorization())
{