Update Google Drive to API v3

This commit is contained in:
SupSuper 2018-04-11 05:30:09 +01:00
parent 9bb156ac6b
commit 1084054dd1
4 changed files with 44 additions and 26 deletions

View file

@ -148,7 +148,7 @@ public virtual void StopUpload()
}
}
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 @@ public virtual void StopUpload()
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

@ -61,7 +61,7 @@ public override GenericUploader CreateUploader(UploadersConfig config, TaskRefer
public enum GoogleDrivePermissionRole
{
owner, reader, writer
owner, reader, writer, organizer, commenter
}
public enum GoogleDrivePermissionType
@ -106,7 +106,7 @@ public bool GetAccessToken(string code)
return GoogleAuth.GetAccessToken(code);
}
private string GetMetadata(string title, string parentID)
private string GetMetadata(string name, string parentID)
{
object metadata;
@ -114,7 +114,7 @@ private string GetMetadata(string title, string parentID)
{
metadata = new
{
title = title,
name = name,
parents = new[]
{
new
@ -128,25 +128,24 @@ 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, GoogleAuth.GetAuthHeaders());
@ -170,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, 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)
@ -192,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: GoogleAuth.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))
{
@ -205,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)
@ -221,7 +232,7 @@ public override UploadResult Upload(Stream stream, string fileName)
}
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 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

@ -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);
@ -920,9 +920,11 @@ private void FTPLoadAccount(FTPAccount account)
case FTPProtocol.FTP:
rbFTPProtocolFTP.Checked = true;
break;
case FTPProtocol.FTPS:
rbFTPProtocolFTPS.Checked = true;
break;
case FTPProtocol.SFTP:
rbFTPProtocolSFTP.Checked = true;
break;
@ -1840,11 +1842,13 @@ private void TestCustomUploader(CustomUploaderDestinationType type, CustomUpload
result.Errors = imageUploader.Errors;
}
break;
case CustomUploaderDestinationType.TextUploader:
CustomTextUploader textUploader = new CustomTextUploader(item);
result = textUploader.UploadText("ShareX text upload test", "Test.txt");
result.Errors = textUploader.Errors;
break;
case CustomUploaderDestinationType.FileUploader:
using (Stream stream = ShareXResources.Logo.GetStream())
{
@ -1853,11 +1857,13 @@ private void TestCustomUploader(CustomUploaderDestinationType type, CustomUpload
result.Errors = fileUploader.Errors;
}
break;
case CustomUploaderDestinationType.URLShortener:
CustomURLShortener urlShortener = new CustomURLShortener(item);
result = urlShortener.ShortenURL(Links.URL_WEBSITE);
result.Errors = urlShortener.Errors;
break;
case CustomUploaderDestinationType.URLSharingService:
CustomURLSharer urlSharer = new CustomURLSharer(item);
result = urlSharer.ShareURL(Links.URL_WEBSITE);

View file

@ -66,7 +66,7 @@ public bool GetAccessToken(string code)
args.Add("redirect_uri", RedirectMethod);
args.Add("grant_type", "authorization_code");
string response = GoogleUploader.SendRequestMultiPart(TokenEndpoint, args);
string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args);
if (!string.IsNullOrEmpty(response))
{
@ -93,7 +93,7 @@ public bool RefreshAccessToken()
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("grant_type", "refresh_token");
string response = GoogleUploader.SendRequestMultiPart(TokenEndpoint, args);
string response = GoogleUploader.SendRequestURLEncoded(HttpMethod.POST, TokenEndpoint, args);
if (!string.IsNullOrEmpty(response))
{