【问题标题】:C# Desktop application. Simple example how to upload a file to Google Drive [closed]C# 桌面应用程序。如何将文件上传到 Google Drive 的简单示例 [关闭]
【发布时间】:2012-08-16 19:26:09
【问题描述】:

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

目前我有:

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 的实例?如果我上面的代码是正确的...... 提前致谢。

【问题讨论】:

标签: c# google-drive-api


【解决方案1】:

这是一个完整的 C# 命令行示例,用于将文件上传到 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 上提供

【讨论】:

  • 我知道这是一个有点旧的帖子。但是我尝试将您的代码用于学习目的。但恐怕我在var service = new DriveService(auth); 中遇到编译错误。编译错误显示为Argument 1: cannot convert from 'Google.Apis.Authentication.OAuth2.OAuth2Authenticator&lt;Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient&gt;' to 'Google.Apis.Services.BaseClientService.Initializer'.你能帮忙吗?
  • 此外,我对更多的概念有点困惑,即使在互联网上阅读后也无法将它们放在脑海中。我看过你的 SO 个人资料,0 个问题和 400 个以上的答案。如果我能在 SO 聊天中花 5 分钟时间来澄清我的一些困惑,我会非常满意。我知道我现在要求太多了:)
  • 所以我修复了它,我使用了错误版本的 dll。现在必须努力消除我的其他困惑
  • @Claudio Cherubino : 它可以集成到 Visual Studio 2008 中
猜你喜欢
  • 2016-04-05
  • 2021-08-30
  • 1970-01-01
  • 2020-03-10
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多