使用C#的Google Drive API - 正在上传

时间:2022-06-20 15:24:35

I am trying to use Google Drive API from an asp.net application to upload files.

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

Problem: The code works locally but when uploaded to server nothing happens (the page just keeps loading...no consent screen showing. Help appreaciated. "UploadedPdf" folder is writable and client_secrets.json exists in the folder.

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

NOTE: I have not installed anything on the server but just uploaded all the files included the Google API dll's into the bin folder of the server.

注意:我没有在服务器上安装任何东西,只是将包含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;

4 个解决方案

#1


2  

FileDatastore puts files by default in %appData%. Normally I would do something like. (lets assume I use "user" as well)

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

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

Then when I call it I get a directory called

然后,当我调用它时,我得到一个名为的目录

%AppData%\Roaming\Console.Analytics.Auth.Store

in that directory now lives a file called

在该目录中现在存在一个名为的文件

Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

I haven't tested what doing

我还没有测试过做什么

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

will do but my guess is you are going to get a directory named

会做,但我的猜测是你要获得一个名为的目录

%AppData%\Roaming\serverpat/uploadpdf/

Which I don't think is what you are after. If that is actually what you are trying to do that you have made that directory writeable? You might want to lookinto using LocalFileDataStore instead.

我不认为你是在追求什么。如果这实际上是你想要做的那样你已经使该目录可写?您可能希望改为使用LocalFileDataStore。

I am not sure if this is your problem or not.

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

Also remember you are hard coding "user" so technically your code is going to ask you to authenticate once for "user" after that it has the authentication in that file for "user" so there is no reason to ask for it again.

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

#2


0  

I think you are calling the AuthorizeAsync() wrong. Use the await keyword and remote the .Result. You'll have to mark the method as async in order to do this.

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

I would also suggest using the UploadAsync() method instead. You don't need to load the entire file into memory with ReadAllBytes() either. Just use File.OpenRead() and pass the FileStream instead of the MemoryStream.

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

Google has some example code: https://developers.google.com/api-client-library/dotnet/guide/media_upload

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

#3


0  

Your code suggests that you are using "Installed Application" workflow/credentials, which is supposed to work fine if you are running your ASP.NET application locally. However, when you move the same code to a web server, its no more an installed application. It enters the category of "Web Application".

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

You need to go to Google Developer Console->Credentials->Create New Client ID and generate a new set of credentials for your web application. Also you need to change the OAuth Flow as per this guide:

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

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

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

#4


0  

steps include

步骤包括

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.

try this,

尝试这个,

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

#1


2  

FileDatastore puts files by default in %appData%. Normally I would do something like. (lets assume I use "user" as well)

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

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

Then when I call it I get a directory called

然后,当我调用它时,我得到一个名为的目录

%AppData%\Roaming\Console.Analytics.Auth.Store

in that directory now lives a file called

在该目录中现在存在一个名为的文件

Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

I haven't tested what doing

我还没有测试过做什么

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

will do but my guess is you are going to get a directory named

会做,但我的猜测是你要获得一个名为的目录

%AppData%\Roaming\serverpat/uploadpdf/

Which I don't think is what you are after. If that is actually what you are trying to do that you have made that directory writeable? You might want to lookinto using LocalFileDataStore instead.

我不认为你是在追求什么。如果这实际上是你想要做的那样你已经使该目录可写?您可能希望改为使用LocalFileDataStore。

I am not sure if this is your problem or not.

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

Also remember you are hard coding "user" so technically your code is going to ask you to authenticate once for "user" after that it has the authentication in that file for "user" so there is no reason to ask for it again.

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

#2


0  

I think you are calling the AuthorizeAsync() wrong. Use the await keyword and remote the .Result. You'll have to mark the method as async in order to do this.

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

I would also suggest using the UploadAsync() method instead. You don't need to load the entire file into memory with ReadAllBytes() either. Just use File.OpenRead() and pass the FileStream instead of the MemoryStream.

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

Google has some example code: https://developers.google.com/api-client-library/dotnet/guide/media_upload

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

#3


0  

Your code suggests that you are using "Installed Application" workflow/credentials, which is supposed to work fine if you are running your ASP.NET application locally. However, when you move the same code to a web server, its no more an installed application. It enters the category of "Web Application".

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

You need to go to Google Developer Console->Credentials->Create New Client ID and generate a new set of credentials for your web application. Also you need to change the OAuth Flow as per this guide:

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

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

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

#4


0  

steps include

步骤包括

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.

try this,

尝试这个,

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