【问题标题】:Google Drive API using C# - Uploading使用 C# 的 Google Drive API - 上传
【发布时间】:2015-07-01 16:02:05
【问题描述】:

我正在尝试使用来自 asp.net 应用程序的 Google Drive API 来上传文件。

问题:代码在本地工作,但上传到服务器时没有任何反应(页面一直在加载...没有显示同意屏幕。帮助 appreaciated。 “UploadedPdf”文件夹是可写的,并且client_secrets.json 存在于该文件夹中。

注意:我没有在服务器上安装任何东西,只是将包含 Google API dll 的所有文件上传到服务器的 bin 文件夹中。

UserCredential credential;
        using (var filestream = new FileStream(Server.MapPath("UploadedPdf/client_secrets.json"),
            FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(filestream).Secrets,
                new[] { DriveService.Scope.Drive },
                "user",
                CancellationToken.None,
                new FileDataStore(Server.MapPath("UploadedPdf/"))).Result;
        }

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

        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
        body.Title = "My document";
        body.Description = "A test document";
        body.MimeType = "text/plain";

        byte[] byteArray = System.IO.File.ReadAllBytes(Server.MapPath("UploadedPdf/sample.txt"));
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
        request.Upload();

        Google.Apis.Drive.v2.Data.File file = request.ResponseBody;

【问题讨论】:

  • 您是否在 Google 开发者控制台中将重定向 uri 添加到服务器?
  • 是的,我确实添加了重定向网址。
  • 转到浏览器控制台任何错误请更新?
  • 检查您是否拥有在服务器中保存文件的完全权限
  • 我真的很生气 Google 不能让这个 API 正常工作。它非常容易出错,很难学习,对于简单的操作来说非常复杂。我想使用云存储作为我的 Blob 存储 (DMS),在这种情况下真的很糟糕。只需 Google 一下,您就会发现数十个关于人们在使用 Drive API 的不同层次上遇到的问题。

标签: c# asp.net google-api google-drive-api google-api-dotnet-client


【解决方案1】:

FileDatastore 默认将文件放在 %appData% 中。通常我会做类似的事情。 (假设我也使用“用户”)

new FileDataStore("Console.Analytics.Auth.Store")  

然后当我调用它时,我会得到一个名为

的目录
%AppData%\Roaming\Console.Analytics.Auth.Store

现在该目录中存在一个名为

的文件
Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

我还没有测试过做什么

new FileDataStore(Server.MapPath("UploadedPdf/"))

可以,但我猜你会得到一个名为

的目录
%AppData%\Roaming\serverpat/uploadpdf/

我认为这不是你所追求的。如果这实际上是您正在尝试做的,那么您已经使该目录可写?您可能想改用LocalFileDataStore

我不确定这是否是您的问题。

还请记住,您正在对“用户”进行硬编码,因此从技术上讲,您的代码会要求您对“用户”进行一次身份验证,之后它在该文件中对“用户”进行身份验证,因此没有理由要求它再次。

【讨论】:

    【解决方案2】:

    我认为您调用 AuthorizeAsync() 是错误的。使用 await 关键字和远程 .Result。您必须将方法标记为异步才能执行此操作。

    我还建议改用 UploadAsync() 方法。您也不需要使用 ReadAllBytes() 将整个文件加载到内存中。只需使用 File.OpenRead() 并传递 FileStream 而不是 MemoryStream。

    Google 有一些示例代码: https://developers.google.com/api-client-library/dotnet/guide/media_upload

    【讨论】:

      【解决方案3】:

      您的代码表明您正在使用“已安装的应用程序”工作流/凭据,如果您在本地运行 ASP.NET 应用程序,它应该可以正常工作。但是,当您将相同的代码移动到 Web 服务器时,它不再是已安装的应用程序。它进入“Web 应用程序”类别。

      您需要转到 Google Developer Console->凭据->创建新的客户端 ID 并为您的 Web 应用程序生成一组新的凭据。您还需要按照本指南更改 OAuth 流程:

      https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc

      【讨论】:

      【解决方案4】:

      步骤包括

      1)Access to the internet and a web browser, in order to authorize the sample   app.
      2)A Google account with Drive enabled.
      3)An environment to run programs in your selected language.
      

      试试这个,

      https://developers.google.com/drive/v2/reference/files/insert#examples
      

      【讨论】:

        猜你喜欢
        • 2020-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 2020-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多