Google Drive V3 and service account for get files list from Google Drive Account (C#)

87 views
Skip to first unread message

Daniele Volpe

unread,
Nov 26, 2017, 5:20:49 PM11/26/17
to Google App Engine
Hi, i'm programming in c# and i want to get the list of files from a Google Drive Account.
The authentication work fine, but when i do: 

FilesResource.ListRequest listRequest = service.Files.List();
var files = listRequest.Execute();


I have the error [401]: Invalid Credentials

Please help me and sorry for my english,
Daniele.

George (Cloud Platform Support)

unread,
Nov 27, 2017, 1:33:40 PM11/27/17
to google-a...@googlegroups.com
Hello Daniele, 

It looks as if credentials have not been set properly for your project. How did you set app authentication, exactly, and how did you ascertain it works fine, as you mention? You can gather more detail by reading points a. to h. at Step 1 on the ".NET Quickstart" documentation page. In fact, following the whole example provided on that page may prove generally helpful in your case. 

The actual code example on this page is somewhat more elaborate than your example: 

// Define parameters of request.
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.PageSize = 10;
            listRequest.Fields = "nextPageToken, files(id, name)";

            // List files.
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                .Files; 

You are encouraged to adapt this code snippet for your particular situation, and post the results back here. 

Daniele Volpe

unread,
Nov 27, 2017, 3:36:52 PM11/27/17
to Google App Engine
Hi George, this is my code.

string[] scopes = new string[] { DriveService.Scope.Drive }; // Full access

 

            var keyFilePath = Environment.CurrentDirectory + "JSON_FILE_LOCATION";   

            if (!System.IO.File.Exists(keyFilePath))

            {

                Console.WriteLine("An Error occurred - Key file does not exist");

            }

 

            try

            {

                using (var stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read))

                {

 

                    var googleCredential = GoogleCredential.FromStream(stream);

                    if (googleCredential.IsCreateScopedRequired)

                    {

                        googleCredential.CreateScoped(scopes);

                    }

 

                    // Create the service.

                    DriveService service = new DriveService(new BaseClientService.Initializer()

                    {

                        HttpClientInitializer = googleCredential,

                        ApplicationName = "APP_NAME",

                    });

 

                    try

                    {

                        FilesResource.ListRequest listRequest = service.Files.List();

                        FileList files = listRequest.Execute();

                    }

                    catch (Exception e)

                    {

                        Console.WriteLine("An error occurred: " + e.Message);

                    }

       

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.InnerException);

            }

 

        }





And this is the exception :


 

Eccezione generata: 'Google.GoogleApiException' in mscorlib.dll

An error occurred: Google.Apis.Requests.RequestError

Invalid Credentials [401]

Errors [

       Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]

]

Message has been deleted

George (Cloud Platform Support)

unread,
Nov 28, 2017, 4:25:45 PM11/28/17
to Google App Engine
What happens if you use the line: 

// List files.
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()

in place of line: 

FileList files = listRequest.Execute();

Daniele Volpe

unread,
Nov 28, 2017, 5:55:09 PM11/28/17
to Google App Engine
Ok, now this code is working but there is a problem.
When the program looks for files inside Google Drive i cant' see my files but only a file called "Getting started".
If i open Google Drive from the website i haven't this file in my cloud, but i can see only my files.... ?
Why ? With the code i look in the wrong directory ?
Thank you.

 ---------------------------------------------------------------------      

            string[] scopes = new string[] { DriveService.Scope.Drive };

 

            GoogleCredential credential;

            using (var stream = new FileStream(JSON_PATH, FileMode.Open, FileAccess.Read))

            {

                credential = GoogleCredential.FromStream(stream);

            }

 

            DriveService service = new DriveService(new BaseClientService.Initializer()

            {

                HttpClientInitializer = credential.CreateScoped(scopes),

                ApplicationName = APPLICATION_NAME

            });

 

            FilesResource.ListRequest listRequest = service.Files.List();

            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;

            if (files != null && files.Count > 0)

                    {

                        foreach (var file in files)

                        {

                            Console.WriteLine("{0} ({1}) {2}", file.Name, file.Id);

                        }

                    }

------------------------------------------------------------------ 


Daniele Volpe

unread,
Nov 29, 2017, 2:55:35 PM11/29/17
to Google App Engine
Now all is working.
I have add the service account email authorization in every files i want to search.

Thank you for the help :)
Message has been deleted
Message has been deleted

George (Cloud Platform Support)

unread,
Nov 30, 2017, 9:55:58 AM11/30/17
to Google App Engine
Hi Daniele!

A more detailed report on your solution might prove of great help for people encountering the same issue in future. 
Reply all
Reply to author
Forward
0 new messages