码迷,mamicode.com
首页 > Windows程序 > 详细

<C#>Google Drive APIs

时间:2014-05-10 19:21:52      阅读:455      评论:0      收藏:0      [点我收藏+]

标签: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。

第三步,编程

bubuko.com,布布扣
 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 GoogleDriveSamples
12 {
13     class DriveCommandLineSample
14     {
15         static void Main(string[] args)
16         {
17             UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
18                 new ClientSecrets
19                 {
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 }
bubuko.com,布布扣

最后,验证
运行程序后(F5),跳出APIs申请权限,点击“接受”,就运行OK。

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

<C#>Google Drive APIs,布布扣,bubuko.com

<C#>Google Drive APIs

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/virgil/p/3720001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!