5

I need to get list of files from google drive with asp.net mvc4 project. Google Drive is used as a file storage for the site. My code:

        const string email = "xxx";
        string path = AppDomain.CurrentDomain.BaseDirectory + "file.p12";
        var certificate = new X509Certificate2(path,"notasecret", X509KeyStorageFlags.Exportable);
        var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(email){
            Scopes = new[] { DriveService.Scope.Drive }

        }.FromCertificate(certificate));
        var service = new DriveService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
            ApplicationName="hikes"
        });
        List<File> result = new List<File>();
        FilesResource.ListRequest request = service.Files.List();
        do
        { 
                FileList files = request.Execute();
                result.AddRange(files.Items);
                request.PageToken = files.NextPageToken;

        } while (!String.IsNullOrEmpty(request.PageToken));

Tried on two different accounts. The result is only one pdf file called "How to get started with Drive". How do I get access to the files of my disk?

  • Why don't you use https://cloud.google.com/storage/ and its client library - https://developers.google.com/api-client-library/dotnet/apis/storage/v1? – peleyal Oct 10 '14 at 14:33
  • I have same another servises, they use google drive. My customer save files from google mail to google drive. There are files need to show in the pages of site – Владимир Рак Oct 10 '14 at 16:16
  • Did you try accessing those files using regular flows and NOT SERVICE ACCOUNT? I'm not that familiar with service account, but you might want to fill the User field, https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.Auth.DotNet4/OAuth2/ServiceAccountCredential.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6a88#62 – peleyal Oct 13 '14 at 15:07

1 Answers1

8

You need to share at least one folder from your own Google drive accounts with the service's account email created in Google console (416132713247-aimfq7...hj37795pcpvo3@developer.gserviceaccount.com): Right click > Share with > fill email & permissions.

Ronan
  • 4,311
  • 3
  • 19
  • 14
  • Also change the query params to `q=sharedWithMe` will list the shared item for this service account. – checksum Apr 14 '15 at 21:36