Google Drive .NET Quickstart Error for Credentials

3

I'm starting to study Google Drive's Api for a personal project in .NET, and I followed the step-by-step guide available on the google website

link

But when I was running the code at the time of obtaining the credentials happened this error:

Hereisthecodeused:

usingGoogle.Apis.Auth.OAuth2;usingGoogle.Apis.Drive.v3;usingGoogle.Apis.Services;usingGoogle.Apis.Util.Store;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Threading;namespaceTestesGoogleDrive{classProgram{//Ifmodifyingthesescopes,deleteyourpreviouslysavedcredentials//at~/.credentials/drive-dotnet-quickstart.jsonstaticstring[]Scopes={DriveService.Scope.Drive};staticstringApplicationName="Drive API .NET Quickstart";

        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
            //    credPath = Path.Combine(credPath, "credentials/drive-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Drive API service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // 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;
            Console.WriteLine("Files:");
            if (files != null && files.Count > 0)
            {
                foreach (var file in files)
                {
                    Console.WriteLine("{0} ({1})", file.Name, file.Id);
                }
            }
            else
            {
                Console.WriteLine("No files found.");
            }
            Console.Read();

        }
    }
}

Packages installed in the project as it is in the tutorial, plus settings in the client_secret.json file to always copy to the output as it is also in the tutorial:

Has this problem ever happened to anyone or anything like it?

    
asked by anonymous 30.05.2017 / 21:52

1 answer

1

I was able to find the problem

My problem was that when I created the path to save the Token: "Users \ NOVA \ Documents.credentials \ drive-dotnet-quickstart.json". Windows was barring project access to create the folder at this location, to save the Token.

So to solve the problem I ran Visual Studio in managed mode. And then yes the project was able to create the folder in the specified destination and everything happened ...

    
31.05.2017 / 13:21