C# 桌面应用程序。如何将文件上传到 Google Drive 的简单示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11994101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Desktop application. Simple example how to upload a file to Google Drive
提问by Alexey
Is there any code example of a desktop application how to authorize to Google Drive service and upload a file?
是否有桌面应用程序如何授权 Google Drive 服务并上传文件的代码示例?
Currently I have:
目前我有:
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
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.
但是接下来的步骤是什么?正如我从示例中看到的,我应该获取 IAuthenticator 实例并将其传递给 DriveService 类的构造函数...如何获取 IAuthenticator 的实例?如果我上面的代码是正确的...提前致谢。
采纳答案by Claudio Cherubino
Here is a complete command-line sample in C# to upload a file to Google Drive:
这是一个完整的 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);
}
}
}
UPDATE: this quickstart sample is now available at https://developers.google.com/drive/quickstart
更新:此快速入门示例现在可在https://developers.google.com/drive/quickstart 上获得

