Google Drive . NET Quickstart Error to get Credentials

Asked

Viewed 156 times

3

I’m starting to study the Google Drive Api for a personal . NET project, and followed the step by step made available on the google website

https://developers.google.com/drive/v3/web/quickstart/dotnet

However when I went to execute the code at the moment of obtaining the credentials this error happened: inserir a descrição da imagem aqui

Below is the code used:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace TestesGoogleDrive
{
    class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/drive-dotnet-quickstart.json
        static string[] Scopes = { DriveService.Scope.Drive };
        static string ApplicationName = "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 in the output as it is also in the tutorial:inserir a descrição da imagem aqui

Has this problem ever happened to someone or something like that ?

1 answer

1


I managed 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 blocking the project access to create the folder in this location, to save the Token.

So to solve the problem I ran Visual Studio in managed mode. And then yes the project managed to create the folder at the specified destination and everything went right...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.