mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
Update Google Drive to API v3
This commit is contained in:
parent
9bb156ac6b
commit
1084054dd1
4 changed files with 44 additions and 26 deletions
|
@ -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)
|
ResponseType responseType = ResponseType.Text)
|
||||||
{
|
{
|
||||||
string query = URLHelpers.CreateQuery(args);
|
string query = URLHelpers.CreateQuery(args);
|
||||||
|
@ -189,7 +189,7 @@ protected bool SendRequestDownload(HttpMethod method, string url, Stream downloa
|
||||||
return false;
|
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)
|
ResponseType responseType = ResponseType.Text)
|
||||||
{
|
{
|
||||||
string boundary = CreateBoundary();
|
string boundary = CreateBoundary();
|
||||||
|
|
|
@ -61,7 +61,7 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
|
||||||
|
|
||||||
public enum GoogleDrivePermissionRole
|
public enum GoogleDrivePermissionRole
|
||||||
{
|
{
|
||||||
owner, reader, writer
|
owner, reader, writer, organizer, commenter
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum GoogleDrivePermissionType
|
public enum GoogleDrivePermissionType
|
||||||
|
@ -106,7 +106,7 @@ public bool GetAccessToken(string code)
|
||||||
return GoogleAuth.GetAccessToken(code);
|
return GoogleAuth.GetAccessToken(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetMetadata(string title, string parentID)
|
private string GetMetadata(string name, string parentID)
|
||||||
{
|
{
|
||||||
object metadata;
|
object metadata;
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ private string GetMetadata(string title, string parentID)
|
||||||
{
|
{
|
||||||
metadata = new
|
metadata = new
|
||||||
{
|
{
|
||||||
title = title,
|
name = name,
|
||||||
parents = new[]
|
parents = new[]
|
||||||
{
|
{
|
||||||
new
|
new
|
||||||
|
@ -128,25 +128,24 @@ private string GetMetadata(string title, string parentID)
|
||||||
{
|
{
|
||||||
metadata = new
|
metadata = new
|
||||||
{
|
{
|
||||||
title = title
|
name = name
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(metadata);
|
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;
|
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
|
string json = JsonConvert.SerializeObject(new
|
||||||
{
|
{
|
||||||
role = role.ToString(),
|
role = role.ToString(),
|
||||||
type = type.ToString(),
|
type = type.ToString(),
|
||||||
value = value,
|
allowFileDiscovery = allowFileDiscovery.ToString()
|
||||||
withLink = withLink.ToString()
|
|
||||||
});
|
});
|
||||||
|
|
||||||
string response = SendRequest(HttpMethod.POST, url, json, ContentTypeJSON, null, GoogleAuth.GetAuthHeaders());
|
string response = SendRequest(HttpMethod.POST, url, json, ContentTypeJSON, null, GoogleAuth.GetAuthHeaders());
|
||||||
|
@ -170,20 +169,32 @@ public List<GoogleDriveFile> GetFolders(bool trashed = false, bool writer = true
|
||||||
|
|
||||||
Dictionary<string, string> args = new Dictionary<string, string>();
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
||||||
args.Add("q", query);
|
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, GoogleAuth.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)
|
public override UploadResult Upload(Stream stream, string fileName)
|
||||||
|
@ -192,8 +203,8 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
|
|
||||||
string metadata = GetMetadata(fileName, FolderID);
|
string metadata = GetMetadata(fileName, FolderID);
|
||||||
|
|
||||||
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart", stream, fileName, headers: GoogleAuth.GetAuthHeaders(),
|
UploadResult result = SendRequestFile("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,webViewLink,webContentLink", stream, fileName,
|
||||||
contentType: "multipart/related", metadata: metadata);
|
headers: GoogleAuth.GetAuthHeaders(), contentType: "multipart/related", metadata: metadata);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(result.Response))
|
if (!string.IsNullOrEmpty(result.Response))
|
||||||
{
|
{
|
||||||
|
@ -205,7 +216,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
|
|
||||||
if (IsPublic)
|
if (IsPublic)
|
||||||
{
|
{
|
||||||
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, "", true);
|
SetPermissions(upload.id, GoogleDrivePermissionRole.reader, GoogleDrivePermissionType.anyone, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DirectLink)
|
if (DirectLink)
|
||||||
|
@ -221,7 +232,7 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result.URL = upload.alternateLink;
|
result.URL = upload.webViewLink;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,14 +244,15 @@ public override UploadResult Upload(Stream stream, string fileName)
|
||||||
public class GoogleDriveFile
|
public class GoogleDriveFile
|
||||||
{
|
{
|
||||||
public string id { get; set; }
|
public string id { get; set; }
|
||||||
public string alternateLink { get; set; }
|
public string webViewLink { get; set; }
|
||||||
public string webContentLink { get; set; }
|
public string webContentLink { get; set; }
|
||||||
public string title { get; set; }
|
public string name { get; set; }
|
||||||
public string description { get; set; }
|
public string description { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GoogleDriveFileList
|
public class GoogleDriveFileList
|
||||||
{
|
{
|
||||||
public List<GoogleDriveFile> items { get; set; }
|
public List<GoogleDriveFile> files { get; set; }
|
||||||
|
public string nextPageToken { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -568,7 +568,7 @@ private void GoogleDriveRefreshFolders()
|
||||||
{
|
{
|
||||||
foreach (GoogleDriveFile folder in folders)
|
foreach (GoogleDriveFile folder in folders)
|
||||||
{
|
{
|
||||||
ListViewItem lvi = new ListViewItem(folder.title);
|
ListViewItem lvi = new ListViewItem(folder.name);
|
||||||
lvi.SubItems.Add(folder.description);
|
lvi.SubItems.Add(folder.description);
|
||||||
lvi.Tag = folder;
|
lvi.Tag = folder;
|
||||||
lvGoogleDriveFoldersList.Items.Add(lvi);
|
lvGoogleDriveFoldersList.Items.Add(lvi);
|
||||||
|
@ -920,9 +920,11 @@ private void FTPLoadAccount(FTPAccount account)
|
||||||
case FTPProtocol.FTP:
|
case FTPProtocol.FTP:
|
||||||
rbFTPProtocolFTP.Checked = true;
|
rbFTPProtocolFTP.Checked = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FTPProtocol.FTPS:
|
case FTPProtocol.FTPS:
|
||||||
rbFTPProtocolFTPS.Checked = true;
|
rbFTPProtocolFTPS.Checked = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FTPProtocol.SFTP:
|
case FTPProtocol.SFTP:
|
||||||
rbFTPProtocolSFTP.Checked = true;
|
rbFTPProtocolSFTP.Checked = true;
|
||||||
break;
|
break;
|
||||||
|
@ -1840,11 +1842,13 @@ private void TestCustomUploader(CustomUploaderDestinationType type, CustomUpload
|
||||||
result.Errors = imageUploader.Errors;
|
result.Errors = imageUploader.Errors;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CustomUploaderDestinationType.TextUploader:
|
case CustomUploaderDestinationType.TextUploader:
|
||||||
CustomTextUploader textUploader = new CustomTextUploader(item);
|
CustomTextUploader textUploader = new CustomTextUploader(item);
|
||||||
result = textUploader.UploadText("ShareX text upload test", "Test.txt");
|
result = textUploader.UploadText("ShareX text upload test", "Test.txt");
|
||||||
result.Errors = textUploader.Errors;
|
result.Errors = textUploader.Errors;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CustomUploaderDestinationType.FileUploader:
|
case CustomUploaderDestinationType.FileUploader:
|
||||||
using (Stream stream = ShareXResources.Logo.GetStream())
|
using (Stream stream = ShareXResources.Logo.GetStream())
|
||||||
{
|
{
|
||||||
|
@ -1853,11 +1857,13 @@ private void TestCustomUploader(CustomUploaderDestinationType type, CustomUpload
|
||||||
result.Errors = fileUploader.Errors;
|
result.Errors = fileUploader.Errors;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CustomUploaderDestinationType.URLShortener:
|
case CustomUploaderDestinationType.URLShortener:
|
||||||
CustomURLShortener urlShortener = new CustomURLShortener(item);
|
CustomURLShortener urlShortener = new CustomURLShortener(item);
|
||||||
result = urlShortener.ShortenURL(Links.URL_WEBSITE);
|
result = urlShortener.ShortenURL(Links.URL_WEBSITE);
|
||||||
result.Errors = urlShortener.Errors;
|
result.Errors = urlShortener.Errors;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CustomUploaderDestinationType.URLSharingService:
|
case CustomUploaderDestinationType.URLSharingService:
|
||||||
CustomURLSharer urlSharer = new CustomURLSharer(item);
|
CustomURLSharer urlSharer = new CustomURLSharer(item);
|
||||||
result = urlSharer.ShareURL(Links.URL_WEBSITE);
|
result = urlSharer.ShareURL(Links.URL_WEBSITE);
|
||||||
|
|
|
@ -66,7 +66,7 @@ public bool GetAccessToken(string code)
|
||||||
args.Add("redirect_uri", RedirectMethod);
|
args.Add("redirect_uri", RedirectMethod);
|
||||||
args.Add("grant_type", "authorization_code");
|
args.Add("grant_type", "authorization_code");
|
||||||
|
|
||||||
string response = GoogleUploader.SendRequestMultiPart(TokenEndpoint, args);
|
string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(response))
|
if (!string.IsNullOrEmpty(response))
|
||||||
{
|
{
|
||||||
|
@ -93,7 +93,7 @@ public bool RefreshAccessToken()
|
||||||
args.Add("client_secret", AuthInfo.Client_Secret);
|
args.Add("client_secret", AuthInfo.Client_Secret);
|
||||||
args.Add("grant_type", "refresh_token");
|
args.Add("grant_type", "refresh_token");
|
||||||
|
|
||||||
string response = GoogleUploader.SendRequestMultiPart(TokenEndpoint, args);
|
string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(response))
|
if (!string.IsNullOrEmpty(response))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue