Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#桌面应用程序。如何将文件上载到Google Drive的简单示例_C#_Google Drive Api - Fatal编程技术网

C#桌面应用程序。如何将文件上载到Google Drive的简单示例

C#桌面应用程序。如何将文件上载到Google Drive的简单示例,c#,google-drive-api,C#,Google Drive Api,是否有任何桌面应用程序的代码示例如何授权到Google Drive服务并上载文件 目前我有: var parameters = new OAuth2Parameters { ClientId = ClientCredentials.ClientId, ClientSecret = Cl

是否有任何桌面应用程序的代码示例如何授权到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
但接下来的步骤是什么?正如我从示例中看到的,我应该获取IAAuthenticator实例并将其传递给DriveService类的构造函数。。。如何获取IAAuthenticator的实例?如果我上面的代码是正确的。。。
提前感谢。

这里是一个完整的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);
        }
    }
}
使用系统;
使用系统诊断;
使用DotNetOpenAuth.OAuth2;
使用Google.api.Authentication.OAuth2;
使用Google.api.Authentication.OAuth2.DotNetOpenAuth;
使用Google.api.Drive.v2;
使用Google.api.Drive.v2.Data;
使用Google.api.Util;
名称空间GoogleDriveSamples
{
类DriveCommandLineSample
{
静态void Main(字符串[]参数)
{
String CLIENT\u ID=“您的\u CLIENT\u ID”;
String CLIENT\u SECRET=“您的\u CLIENT\u SECRET”;
//注册验证器并创建服务
var provider=新的本地应用程序客户端(GoogleAuthenticationServer.Description,CLIENT\u ID,CLIENT\u SECRET);
var auth=新的OAuth2Authenticator(提供者,GetAuthorization);
{
验证器=auth
});
文件体=新文件();
body.Title=“我的文档”;
body.Description=“测试文档”;
body.MimeType=“text/plain”;
byte[]byteArray=System.IO.File.ReadAllBytes(“document.txt”);
System.IO.MemoryStream stream=新的System.IO.MemoryStream(byteArray);
fileResource.InsertMediaUpload请求=service.Files.Insert(正文、流、“文本/普通”);
request.Upload();
File File=request.ResponseBody;
Console.WriteLine(“文件id:+File.id”);
Console.ReadLine();
}
私有静态IAAuthorizationState GetAuthorization(NativeApplicationClient参数)
{
//获取身份验证URL:
IAAuthorizationState=新授权状态(新[]{DriveService.Scopes.Drive.GetStringValue()});
state.Callback=新Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri=arg.RequestUserAuthorization(状态);
//请求用户授权(通过打开浏览器窗口):
Process.Start(authUri.ToString());
控制台。写入(“授权代码:”);
字符串authCode=Console.ReadLine();
Console.WriteLine();
//使用授权代码检索访问令牌:
返回参数ProcessUserAuthorization(authCode,state);
}
}
}

更新:这个快速入门示例现在可以在

上找到,可能重复:对于任何喜欢保持简单的人,我编写了一个简单的包装器,支持fluent api For drive命令。我知道这是一个有点旧的职位。但是我试着用你的代码来学习。但是我担心我在
var service=newdriveservice(auth)中遇到编译错误。编译错误显示为
参数1:无法从“Google.api.Authentication.OAuth2.OAuth2Authenticator”转换为“Google.api.Services.BaseClientService.Initializer”。
你能帮我吗?此外,我对其他几个概念有点困惑,即使在互联网上阅读后也无法理解它们。我看过你的SO档案,0个问题,400多个答案。如果我能占用你5分钟的时间来澄清我的困惑,我会很高兴。我知道我现在要求太多了:)所以我修复了它,我使用了错误的dll版本。现在我必须努力抹去我的另一个confusions@ClaudioCherubino:它是否可以集成到visual studio 2008或更近版本(v3)中: