Google Drive API使用C#-上传

本文关键字:C#- 上传 使用 API Drive Google | 更新日期: 2023-09-27 18:00:44

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

问题:代码在本地工作,但当上传到服务器时,什么都没有发生(页面一直在加载……没有显示同意屏幕。帮助得到了回应。"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 Drive API使用C#-上传

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进行查找。

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

还要记住,你对"用户"进行了硬编码,所以从技术上讲,你的代码会要求你为"用户"验证一次,之后它在该文件中为"用户"进行了验证,所以没有理由再次要求它。

我认为您调用AuthorizeAsync()是错误的。使用await关键字并远程调用.Result。为此,您必须将该方法标记为async。

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

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

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

您需要转到Google Developer Console->Credentials->Create New Client ID并为您的web应用程序生成一组新的凭据。此外,您还需要根据以下指南更改OAuth流:

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-应用程序aspnet mvc

步骤包括

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