Created
November 4, 2016 14:34
-
-
Save lamwaihen/968f577baea6eef0c123c4a83c263d67 to your computer and use it in GitHub Desktop.
Google Drive API v3 file upload using C# for UWP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task<File> UploadAsync(DriveService driveService, IList<string> parents, StorageFile file) | |
{ | |
// Prepare the JSON metadata | |
string json = "{\"name\":\"" + file.Name + "\""; | |
if (parents.Count > 0) | |
{ | |
json += ", \"parents\": ["; | |
foreach (string parent in parents) | |
{ | |
json += "\"" + parent + "\", "; | |
} | |
json = json.Remove(json.Length - 2) + "]"; | |
} | |
json += "}"; | |
Debug.WriteLine(json); | |
File uploadedFile = null; | |
try | |
{ | |
BasicProperties prop = await file.GetBasicPropertiesAsync(); | |
ulong fileSize = prop.Size; | |
// Step 1: Start a resumable session | |
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable"); | |
httpRequest.Headers["Content-Type"] = "application /json; charset=UTF-8"; | |
httpRequest.Headers["Content-Length"] = json.Length.ToString(); | |
httpRequest.Headers["X-Upload-Content-Type"] = file.ContentType; | |
httpRequest.Headers["X-Upload-Content-Length"] = fileSize.ToString(); | |
httpRequest.Headers["Authorization"] = "Bearer " + ((UserCredential)driveService.HttpClientInitializer).Token.AccessToken; | |
httpRequest.Method = "POST"; | |
using (System.IO.Stream requestStream = await httpRequest.GetRequestStreamAsync()) | |
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(requestStream)) | |
{ | |
streamWriter.Write(json); | |
} | |
// Step 2: Save the resumable session URI | |
HttpWebResponse httpResponse = (HttpWebResponse)(await httpRequest.GetResponseAsync()); | |
if (httpResponse.StatusCode != HttpStatusCode.OK) | |
return null; | |
// Step 3: Upload the file | |
httpRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=" + httpResponse.Headers["x-guploader-uploadid"]); | |
httpRequest.Headers["Content-Type"] = file.ContentType; | |
httpRequest.Headers["Content-Length"] = fileSize.ToString(); | |
httpRequest.Method = "PUT"; | |
using (System.IO.Stream requestStream = await httpRequest.GetRequestStreamAsync()) | |
using (System.IO.FileStream fileStream = new System.IO.FileStream(file.Path, System.IO.FileMode.Open)) | |
{ | |
await fileStream.CopyToAsync(requestStream); | |
} | |
httpResponse = (HttpWebResponse)(await httpRequest.GetResponseAsync()); | |
if (httpResponse.StatusCode != HttpStatusCode.OK) | |
return null; | |
// Try to retrieve the file from Google | |
FilesResource.ListRequest request = driveService.Files.List(); | |
if (parents.Count > 0) | |
request.Q += "'" + parents[0] + "' in parents and "; | |
request.Q += "name = '" + file.Name + "'"; | |
FileList result = request.Execute(); | |
if (result.Files.Count > 0) | |
uploadedFile = result.Files[0]; | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex.Message); | |
} | |
return uploadedFile; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much for your help :)
I was able to use the Google Drive API successfully to upload files but I noticed something strange,
I tried to upload a file in the same way to a folder in Share Drive and received an error that the ID of the folder was not successfully found. (The service account was shared in the folder)
Do you have any idea why it would work on a personal Drive account and not on a Share Drive??
In terms of the Google Drive API, is there any difference between a personal Drive account and a Share Drive account??