Atihska February 2016

Using google drive sdk, unable to upload a file: Response coming to null

I have a simple code below to upload to google drive but i get response = null everytime. My authentication works and I am able to list the files but not upload or create directory. Do I need some extra permissions? I have seen some samples and I am following them but still my upload doesn't work.

It will be of great help if someone could correct me. Below is my upload code.

public static Google.Apis.Drive.v2.Data.File uploadFile(DriveService service, string uploadFile, string parent = null)
            {
                if (System.IO.File.Exists(uploadFile))
                {                
                    Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
                    body.Title = System.IO.Path.GetFileName(uploadFile);
                    body.Description = "File uploaded by installed app";
                    body.MimeType = GetMimeType(uploadFile);
                    //body.Parents = new List<ParentReference>() { new ParentReference() { Id = parent } };

                    // File's content
                    byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

                    try
                    {
                        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, GetMimeType(uploadFile));
                        //request.Convert = true;
                        request.Upload();
                        Google.Apis.Drive.v2.Data.File f = request.ResponseBody;
                        Console.WriteLine(f.Id);
                        return f;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("An error occured: " + ex.Message);
                        return null;
                    }
                }
                else
                {
                    Console.WriteLine        

Answers


adjuremods February 2016

Uploading files to Google Drive will require you to use the Drive REST API. Since you already have an authentication token, you'll just need to call the POST URL and other parameters that are needed to be set.

A simple upload case will look like this

POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token

JPEG data

There are other uploadTypes to choose from, so just pick up something that will benefit you the best. More information about Uploading files can be seen at the V2 Official Documentation. A sample code is also provided in their github repository (though this is in JS).

Hope this helps!


Atihska February 2016

I was able to solve this. I was not passing enough scopes but just DriveService.Scope.Drive. I changed from string[] scopes = new string[] { DriveService.Scope.Drive }; to

string[] scopes = new string[] { DriveService.Scope.Drive,  
                           DriveService.Scope.DriveAppdata,
                           DriveService.Scope.DriveAppsReadonly,      
                           DriveService.Scope.DriveFile,   
                           DriveService.Scope.DriveMetadataReadonly, 
                           DriveService.Scope.DriveReadonly,      
                           DriveService.Scope.DriveScripts }; 

Post Status

Asked in February 2016
Viewed 3,420 times
Voted 13
Answered 2 times

Search




Leave an answer