C#桌面应用程序。 简单的例子,如何将文件上传到谷歌驱动器[关闭](C# Desktop app

2019-06-27 02:55发布

是否有一个桌面应用程序如何授权给谷歌云端硬盘服务和上传文件的任何代码的例子吗?

目前,我有:

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的实例? 如果我上面的代码是正确的...在此先感谢。

Answer 1:

这是在C#中一个完整的命令行样本文件上传到谷歌驱动器:

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:此快速入门样品现已在https://developers.google.com/drive/quickstart



文章来源: C# Desktop application. Simple example how to upload a file to Google Drive [closed]