<C#>Google Drive APIs

來源:互聯網
上載者:User

標籤:des   style   blog   class   code   java   

第一步,啟用Drive API

首先,註冊Google帳號;其次,登入Google Developers Console;接著,建立工程和程式;緊接,啟用APIs & auth;最後,選擇Credentials。

第二步,安裝Google Client Library

安裝一個NuGet包(Google.Apis.drive)。如在VS2012上,先選擇Tools,再NuGet Package Manager,接著Package Manager Console。在PM>中輸入Install-Package Google.Apis -Pre,Install-Package Google.Apis.Authentication -Pre,Install-Package Google.Apis.Drive.v2 -Pre。

第三步,編程

 1 using System; 2 using System.Threading; 3 using System.Threading.Tasks; 4  5 using Google; 6 using Google.Apis.Auth.OAuth2; 7 using Google.Apis.Drive.v2; 8 using Google.Apis.Drive.v2.Data; 9 using Google.Apis.Services;10 11 namespace GoogleDriveSamples12 {13     class DriveCommandLineSample14     {15         static void Main(string[] args)16         {17             UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(18                 new ClientSecrets19                 {20                     ClientId = "CLIENT_ID_HERE",21                     ClientSecret = "CLIENT_SECRET_HERE",22                 },23                 new[] { DriveService.Scope.Drive },24                 "user",25                 CancellationToken.None).Result;26 27             // Create the service.28             var service = new DriveService(new BaseClientService.Initializer()29             {30                 HttpClientInitializer = credential,31                 ApplicationName = "Drive API Sample",32             });33 34             File body = new File();35             body.Title = "My document";36             body.Description = "A test document";37             body.MimeType = "text/plain";38 39             byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");40             System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);41 42             FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");43             request.Upload();44 45             File file = request.ResponseBody;46             Console.WriteLine("File id: " + file.Id);47             Console.WriteLine("Press Enter to end this process.");48             Console.ReadLine();49         }50     }51 }

最後,驗證
運行程式後(F5),跳出APIs申請許可權,點擊“接受”,就運行OK。

摘自:https://developers.google.com/drive/web/quickstart/quickstart-cs

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.