当前位置: 首页 > 编程语言 > C#

C#桌面应用程序,如何将文件上传到GoogleDriveshare

时间:2023-04-11 00:39:43 C#

C#桌面应用程序的简单示例。如何将文件上传到GoogleDrive的简单示例是否有桌面应用程序如何授权GoogleDrive服务和上传文件的代码示例?目前我有:varparameters=newOAuth2Parameters{ClientId=ClientCredentials.ClientId,ClientSecret=ClientCredentials.ClientSecret,RedirectUri=ClientCredentials.RedirectUris[0],Scope=ClientCredentials.GetScopes()};//打开url,点击允许,复制密码参数.AccessCode=secretCodeFromBrowser;OAuthUtil.GetAccessToken(参数);字符串accessToken=parameters.AccessToken;//因此,有了访问令牌,但接下来的步骤是什么?从示例中可以看出,我应该获取IAuthenticator实例并将其传递给DriveService类的构造函数...如何获取IAuthenticator实例?如果我上面的代码是正确的...在此先感谢。以下是C#中使用于将文件上传到Google云端硬盘的完整命令执行示例:usingSystem;使用系统诊断;使用DotNetOpenAuth.OAuth2;使用Google.Apis.Authentication.OAuth2;使用Google.Apis.Authentication.OAuth2.DotNetOpenAuth;使用Google.Apis.Drive.v2;使用Google.Apis.Drive.v2.Data;使用Google.Apis.Util;namespaceGoogleDriveSamples{classDriveCommandLineSample{staticvoidMain(string[]args){StringCLIENT_ID="YOUR_CLIENT_ID";StringCLIENT_SECRET="YOUR_CLIENT_SECRET";//注册验证器并创建服务varprovider=newNativeApplicationClient(GoogleAuthenticationServer.Description,CLIENT_ID,CLIENT_SECRET);varauth=newOAuth2Authenticator(provider,GetAuthorization);{认证器=auth});文件体=newFile();body.Title="我的文档";body.Description="一个测试文档";body.MimeType="文本/纯文本";byte[]byteArray=System.IO.File.ReadAllBytes("document.txt");System.IO.MemoryStream流=newSystem.IO.MemoryStream(byteArray);FilesResource.InsertMediaUploadrequest=service.Files.Insert(body,stream,"text/plain");请求.上传();文件file=request.ResponseBody;Console.WriteLine("文件编号:"+file.Id);控制台.ReadLine();}privatestaticIAuthorizationStateGetAuthorization(NativeApplicationClientarg){//获取授权URL:IAuthorizationStatestate=newAuthorizationState(new[]{DriveService.Scopes.Drive.GetStringValue()});state.Callback=newUri(NativeApplicationClient.OutOfBandCallbackUrl);UriauthUri=arg.RequestUserAuthorization(state);//请求用户授权(通过打开浏览器窗口):Process.Start(authUri.ToString());Console.Write("授权码:");字符串authCode=Console.ReadLine();控制台.WriteLine();//使用授权码获取访问令牌:returnarg.ProcessUserAuthorization(authCode,state);}}}更新:此快速入门示例现在已经在https://developers.google.com/drive/quickstart上提供上记就是C#学习教程:一个C#桌面应用程序如何上传文件到GoogleDrive的简单例子代位,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: