Merge pull request #3302 from SupSuper/google-api

Update Google API URLs
This commit is contained in:
Jaex 2018-04-12 10:20:01 +03:00 committed by GitHub
commit 1bb564e66b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 114 additions and 285 deletions

View file

@ -148,7 +148,7 @@ protected string SendRequest(HttpMethod method, string url, string content, stri
}
}
protected string SendRequestURLEncoded(HttpMethod method, string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
public string SendRequestURLEncoded(HttpMethod method, string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
ResponseType responseType = ResponseType.Text)
{
string query = URLHelpers.CreateQuery(args);
@ -189,7 +189,7 @@ protected bool SendRequestDownload(HttpMethod method, string url, Stream downloa
return false;
}
public string SendRequestMultiPart(string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
protected string SendRequestMultiPart(string url, Dictionary<string, string> args, NameValueCollection headers = null, CookieCollection cookies = null,
ResponseType responseType = ResponseType.Text)
{
string boundary = CreateBoundary();

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;
@ -62,7 +61,7 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public enum GoogleDrivePermissionRole
{
owner, reader, writer
owner, reader, writer, organizer, commenter
}
public enum GoogleDrivePermissionType
@ -72,110 +71,42 @@ 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();
}
private string GetMetadata(string title, string parentID)
public bool GetAccessToken(string code)
{
return GoogleAuth.GetAccessToken(code);
}
private string GetMetadata(string name, string parentID)
{
object metadata;
@ -183,7 +114,7 @@ private string GetMetadata(string title, string parentID)
{
metadata = new
{
title = title,
name = name,
parents = new[]
{
new
@ -197,28 +128,27 @@ private string GetMetadata(string title, string parentID)
{
metadata = new
{
title = title
name = name
};
}
return JsonConvert.SerializeObject(metadata);
}
private void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, string value, bool withLink)
private void SetPermissions(string fileID, GoogleDrivePermissionRole role, GoogleDrivePermissionType type, bool allowFileDiscovery)
{
if (!CheckAuthorization()) return;
string url = string.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions", fileID);
string url = string.Format("https://www.googleapis.com/drive/v3/files/{0}/permissions", fileID);
string json = JsonConvert.SerializeObject(new
{
role = role.ToString(),
type = type.ToString(),
value = value,
withLink = withLink.ToString()
allowFileDiscovery = allowFileDiscovery.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)
@ -239,20 +169,32 @@ public List<GoogleDriveFile> GetFolders(bool trashed = false, bool writer = true
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("q", query);
args.Add("fields", "nextPageToken,files(id,name,description)");
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v2/files", args, GetAuthHeaders());
List<GoogleDriveFile> folders = new List<GoogleDriveFile>();
string pageToken = "";
if (!string.IsNullOrEmpty(response))
// Make sure we get all the pages of results
do
{
GoogleDriveFileList fileList = JsonConvert.DeserializeObject<GoogleDriveFileList>(response);
args["pageToken"] = pageToken;
string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v3/files", args, GoogleAuth.GetAuthHeaders());
pageToken = "";
if (fileList != null)
if (!string.IsNullOrEmpty(response))
{
return fileList.items;
GoogleDriveFileList fileList = JsonConvert.DeserializeObject<GoogleDriveFileList>(response);
if (fileList != null)
{
folders.AddRange(fileList.files);
pageToken = fileList.nextPageToken;
}
}
}
while (!string.IsNullOrEmpty(pageToken));
return null;
return folders;
}
public override UploadResult Upload(Stream stream, string fileName)
@ -261,8 +203,8 @@ 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(),
contentType: "multipart/related", metadata: metadata);
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink,webContentLink", stream, fileName,
headers: GoogleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadata);
if (!string.IsNullOrEmpty(result.Response))
{
@ -274,7 +216,7 @@ public override UploadResult Upload(Stream stream, string fileName)
if (IsPublic)
{
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, "", true);
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, false);
}
if (DirectLink)
@ -290,7 +232,7 @@ public override UploadResult Upload(Stream stream, string fileName)
}
else
{
result.URL = upload.alternateLink;
result.URL = upload.webViewLink;
}
}
}
@ -302,14 +244,15 @@ public override UploadResult Upload(Stream stream, string fileName)
public class GoogleDriveFile
{
public string id { get; set; }
public string alternateLink { get; set; }
public string webViewLink { get; set; }
public string webContentLink { get; set; }
public string title { get; set; }
public string name { get; set; }
public string description { get; set; }
}
public class GoogleDriveFileList
{
public List<GoogleDriveFile> items { get; set; }
public List<GoogleDriveFile> files { get; set; }
public string nextPageToken { get; set; }
}
}

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"
};
}

View file

@ -738,6 +738,7 @@ public void LoadSettings()
#region Google URL Shortener
atcGoogleURLShortenerAccountType.SelectedAccountType = Config.GoogleURLShortenerAccountType;
oauth2GoogleURLShortener.Enabled = Config.GoogleURLShortenerAccountType == AccountType.User;
if (OAuth2Info.CheckOAuth(Config.GoogleURLShortenerOAuth2Info))
{
@ -3069,6 +3070,7 @@ private void txtBitlyDomain_TextChanged(object sender, EventArgs e)
private void atcGoogleURLShortenerAccountType_AccountTypeChanged(AccountType accountType)
{
Config.GoogleURLShortenerAccountType = accountType;
oauth2GoogleURLShortener.Enabled = accountType == AccountType.User;
}
private void oauth2GoogleURLShortener_OpenButtonClicked()

View file

@ -568,7 +568,7 @@ private void GoogleDriveRefreshFolders()
{
foreach (GoogleDriveFile folder in folders)
{
ListViewItem lvi = new ListViewItem(folder.title);
ListViewItem lvi = new ListViewItem(folder.name);
lvi.SubItems.Add(folder.description);
lvi.Tag = folder;
lvGoogleDriveFoldersList.Items.Add(lvi);

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.SendRequestURLEncoded(HttpMethod.POST, 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.SendRequestURLEncoded(HttpMethod.POST, 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)