1

I have a C# web application and I want clients to allow files insert/ update/ delete files with automatic authentication using google api client library. My first question is whether it is possible possible or not using "Service account" or any other technique.

I have started things like:

  1. Enabled 2 APIs Drive API and Admin SD
  2. Created a project in the Google Developers Console
  3. Created API keys and OAuth 2.0 client IDs for the project
  4. Installed PM> Install-Package Google.Apis.Drive.v2 for my web app. The dll version is showing v4.0.30319 and my .net framework is 4.0
Kara
  • 6,115
  • 16
  • 50
  • 57
Gopal Biswas
  • 409
  • 1
  • 7
  • 34

1 Answers1

1

If you want to modify a user's Drive, you will need the user to be at least connected and also that he gives to you the permissions to do so.

The best way to learn how to do this is the Google Drive API, they provide a good start-up and a lot of samples for the tasks you ask for : Google Drive REST API, .NET quickstart.

If you still hits problems, there is also others way to learn : a simple google research gives good results. For exemple, Daimto have a tutorial for exactly what you need :

  • Authentication
  • Retrieve files informations
  • Upload, update, delete files
  • Finally a project sample

If you still hit a problem, a little research helps a lot, then ask question here.

Kariamoss
  • 542
  • 1
  • 9
  • 29
  • I'm trying to develop the google service account as mentioned in the link "daimto.com/google-drive-api-with-a-service-account" and it is work fine for me. One problem I'm facing, Suppose a folder has created in google drive named "UploadDocs" and I'm uploading files under this folder. The files are uploaded inside the folder successfully but I couldn't see those uploaded files at all. Is this happening for permissions for the folder "UploadDocs"? Please help – Gopal Biswas May 16 '16 at 12:55
  • It can't be a permission problem if `UploadDocs` is a folder that you create yourself. How do you upload your files ? – Kariamoss May 16 '16 at 14:16
  • I'm using google drive service account. I have created a folder using the code- File NewDirectory = null; File body = new File(); body.Title = _title; body.Description = _description; body.MimeType = "application/vnd.google-apps.folder"; body.Parents = new List() { new ParentReference() { Id = _parent } }; FilesResource.InsertRequest request = _service.Files.Insert(body); NewDirectory = request.Execute(); Then I have uploaded files using the below mentioned code – Gopal Biswas May 17 '16 at 04:47
  • File body = new File(); body.Title = "t"; body.Description = "a"; body.MimeType = GetMimeType(_updFile); body.Parents = new List() { new ParentReference() { Id = _parent } }; byte[] byteArray = System.IO.File.ReadAllBytes(_updFile); System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_updFile)); request.Upload(); – Gopal Biswas May 17 '16 at 04:55
  • After folder creation and file upload both are executing successfully. The data fetching code IList Files = new List(); FilesResource.ListRequest list = service.Files.List(); FileList filesFeed = list.Execute(); The data fetching code also populates the newly uploaded file with id/ title/ description etc. but I couldn't see either the programmatically created folder or the uploaded files in my google drive manually. – Gopal Biswas May 17 '16 at 05:02
  • Did you go go through the regular OAuth 2.0 flow to retrieve credentials from the user ? – Kariamoss May 17 '16 at 09:59
  • No. Ihave used the Google Drive API with a service account. I have done what exactly mentioned in the following url: http://www.daimto.com/google-drive-api-with-a-service-account/ – Gopal Biswas May 17 '16 at 10:34
  • Oh I see your issue. Here is an other question about it : http://stackoverflow.com/questions/12211859/i-cant-see-the-files-and-folders-created-via-code-in-my-google-drive And here is the answer that you need : If you would like to see the files under a User Account without using OAuth 2.0, then you can give the Service account edit permission to a folder under the User Account. Then when uploading the file using the Service Account make sure you set Parent Reference ID to use that folder ID The folder ID is determined in the address bar – Kariamoss May 17 '16 at 13:39
  • 1
    Thanks for your valuable time and opinion. Permissions required for both folder and files. Thanks – Gopal Biswas May 18 '16 at 04:21
  • Perfect, glad to ear it – Kariamoss May 18 '16 at 11:31