<i id='ySEYm'><tr id='ySEYm'><dt id='ySEYm'><q id='ySEYm'><span id='ySEYm'><b id='ySEYm'><form id='ySEYm'><ins id='ySEYm'></ins><ul id='ySEYm'></ul><sub id='ySEYm'></sub></form><legend id='ySEYm'></legend><bdo id='ySEYm'><pre id='ySEYm'><center id='ySEYm'></center></pre></bdo></b><th id='ySEYm'></th></span></q></dt></tr></i><div id='ySEYm'><tfoot id='ySEYm'></tfoot><dl id='ySEYm'><fieldset id='ySEYm'></fieldset></dl></div>

      <legend id='ySEYm'><style id='ySEYm'><dir id='ySEYm'><q id='ySEYm'></q></dir></style></legend>

          <bdo id='ySEYm'></bdo><ul id='ySEYm'></ul>
      1. <small id='ySEYm'></small><noframes id='ySEYm'>

      2. <tfoot id='ySEYm'></tfoot>
      3. C# 桌面应用程序.如何将文件上传到 Google Drive 的

        时间:2023-08-28

        1. <tfoot id='i9EjE'></tfoot>

              <legend id='i9EjE'><style id='i9EjE'><dir id='i9EjE'><q id='i9EjE'></q></dir></style></legend>
            1. <small id='i9EjE'></small><noframes id='i9EjE'>

                  <bdo id='i9EjE'></bdo><ul id='i9EjE'></ul>
                    <tbody id='i9EjE'></tbody>

                  <i id='i9EjE'><tr id='i9EjE'><dt id='i9EjE'><q id='i9EjE'><span id='i9EjE'><b id='i9EjE'><form id='i9EjE'><ins id='i9EjE'></ins><ul id='i9EjE'></ul><sub id='i9EjE'></sub></form><legend id='i9EjE'></legend><bdo id='i9EjE'><pre id='i9EjE'><center id='i9EjE'></center></pre></bdo></b><th id='i9EjE'></th></span></q></dt></tr></i><div id='i9EjE'><tfoot id='i9EjE'></tfoot><dl id='i9EjE'><fieldset id='i9EjE'></fieldset></dl></div>

                  本文介绍了C# 桌面应用程序.如何将文件上传到 Google Drive 的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否有桌面应用程序如何授权 Google Drive 服务和上传文件的代码示例?

                  Is there any code example of a desktop application how to authorize to Google Drive service and upload a file?

                  目前我有:

                  var parameters = new OAuth2Parameters
                                                   {
                                                       ClientId = ClientCredentials.ClientId,
                                                       ClientSecret = ClientCredentials.ClientSecret,
                                                       RedirectUri = ClientCredentials.RedirectUris[0],
                                                       Scope = ClientCredentials.GetScopes()
                                                   };    
                  string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
                      // Open url, click to allow and copy secret code
                      parameters.AccessCode = secretCodeFromBrowser;
                      OAuthUtil.GetAccessToken(parameters);
                      string accessToken = parameters.AccessToken;
                      // So, there is the access token
                  

                  但是接下来的步骤是什么?正如我从示例中看到的那样,我应该获取 IAuthenticator 实例并将其传递给 DriveService 类的构造函数......如何获取 IAuthenticator 的实例?如果我上面的代码是正确的......提前致谢.

                  But what are the next steps? As I see from examples I should get IAuthenticator instance and pass it into constructor of DriveService class... How to get an instance of IAuthenticator? If my above code is correct... Thanks in advance.

                  推荐答案

                  这是一个完整的 C# 命令行示例,用于将文件上传到 Google Drive:

                  Here is a complete command-line sample in C# to upload a file to Google Drive:

                  using System;
                  using System.Diagnostics;
                  using DotNetOpenAuth.OAuth2;
                  using Google.Apis.Authentication.OAuth2;
                  using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
                  using Google.Apis.Drive.v2;
                  using Google.Apis.Drive.v2.Data;
                  using Google.Apis.Util;
                  
                  namespace GoogleDriveSamples
                  {
                      class DriveCommandLineSample
                      {
                          static void Main(string[] args)
                          {
                              String CLIENT_ID = "YOUR_CLIENT_ID";
                              String CLIENT_SECRET = "YOUR_CLIENT_SECRET";
                  
                              // Register the authenticator and create the service
                              var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
                              var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
                              {
                                  Authenticator = auth
                              });
                  
                              File body = new File();
                              body.Title = "My document";
                              body.Description = "A test document";
                              body.MimeType = "text/plain";
                  
                              byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
                              System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                  
                              FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
                              request.Upload();
                  
                              File file = request.ResponseBody;
                              Console.WriteLine("File id: " + file.Id);
                              Console.ReadLine();
                          }
                  
                          private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
                          {
                              // Get the auth URL:
                              IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
                              state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
                              Uri authUri = arg.RequestUserAuthorization(state);
                  
                              // Request authorization from the user (by opening a browser window):
                              Process.Start(authUri.ToString());
                              Console.Write("  Authorization Code: ");
                              string authCode = Console.ReadLine();
                              Console.WriteLine();
                  
                              // Retrieve the access token by using the authorization code:
                              return arg.ProcessUserAuthorization(authCode, state);
                          }
                      }
                  }
                  

                  更新:此快速入门示例现已在 https://developers.google.com/drive/quickstart 上提供

                  UPDATE: this quickstart sample is now available at https://developers.google.com/drive/quickstart

                  这篇关于C# 桌面应用程序.如何将文件上传到 Google Drive 的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 C# 以编程方式定位我的 Google Drive 文件 下一篇:如何通过 Google Drive .NET API v3 使用服务帐户访问

                  相关文章

                  最新文章

                  • <bdo id='osOWO'></bdo><ul id='osOWO'></ul>
                  <tfoot id='osOWO'></tfoot>
                  1. <small id='osOWO'></small><noframes id='osOWO'>

                      <legend id='osOWO'><style id='osOWO'><dir id='osOWO'><q id='osOWO'></q></dir></style></legend>
                    1. <i id='osOWO'><tr id='osOWO'><dt id='osOWO'><q id='osOWO'><span id='osOWO'><b id='osOWO'><form id='osOWO'><ins id='osOWO'></ins><ul id='osOWO'></ul><sub id='osOWO'></sub></form><legend id='osOWO'></legend><bdo id='osOWO'><pre id='osOWO'><center id='osOWO'></center></pre></bdo></b><th id='osOWO'></th></span></q></dt></tr></i><div id='osOWO'><tfoot id='osOWO'></tfoot><dl id='osOWO'><fieldset id='osOWO'></fieldset></dl></div>