使用Google Drive .NET API的Null响应创建文件 [英] Null response creating file using Google Drive .NET API

查看:99
本文介绍了使用Google Drive .NET API的Null响应创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Drive .NET API v3将文件上传到我的云端硬盘.我的代码在下面

I am trying to upload a file onto my Drive using Google Drive .NET API v3. My code is below

static string[] Scopes = { DriveService.Scope.Drive,
                       DriveService.Scope.DriveAppdata,
                       DriveService.Scope.DriveFile,
                       DriveService.Scope.DriveMetadataReadonly,
                       DriveService.Scope.DriveReadonly,
                       DriveService.Scope.DriveScripts };
    static string ApplicationName = "Drive API .NET Quickstart";

    public ActionResult Index()
    {
        UserCredential credential;

        using (var stream =
            new FileStream("C:/Users/admin1/Documents/visual studio 2017/Projects/TryGoogleDrive/TryGoogleDrive/client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = 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;
            Debug.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;
        Debug.WriteLine("Files:");
        if (files != null && files.Count > 0)
        {
            foreach (var file in files)
            {
                Debug.WriteLine("{0} ({1})", file.Name, file.Id);
            }
        }
        else
        {
            Debug.WriteLine("No files found.");
        }

        var fileMetadata = new Google.Apis.Drive.v3.Data.File()
        {
            Name = "report.csv",
            MimeType = "text/csv",
        };
        FilesResource.CreateMediaUpload request;
        using (var stream = new FileStream("C:/debugging/report.csv",
                                FileMode.Open))
        {
            request = service.Files.Create(
                fileMetadata, stream, "text/csv");
            request.Fields = "id";
            request.Upload();
        }
        var response = request.ResponseBody;
        Console.WriteLine("File ID: " + response.Id);

        return View();        
    }

我面临的问题是响应始终为null.我进一步调查了一下,发现该请求返回了403 resultCode.我还查看了,但是它们都没有任何帮助.

The problem I'm facing is that response is always null. I looked into it a bit further and found that the request returned a 403 resultCode. I also took a look at some other questions on SO this and this but neither were of any help.

我忘了提到代码的第一部分工作正常-它列出了我的云端硬盘中的所有文件.只有第二部分不起作用(上传文件部分)

I forgot to mention that the first part of the code is working correctly - it lists all the files in my Drive. Only the second part is not working (the upload file part)

推荐答案

    string[] Scopes = { DriveService.Scope.Drive };

  • 更改驱动器作用域,然后删除文件token.json
  • 在vs2017中,当存在client_secret.json文件时,您可以在token.json文件夹中看到token.json文件.
  • 这篇关于使用Google Drive .NET API的Null响应创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆