{"id":3962,"date":"2017-11-08T08:00:33","date_gmt":"2017-11-07T23:00:33","guid":{"rendered":"https:\/\/embedded-property.net\/?p=3962"},"modified":"2017-11-07T22:41:21","modified_gmt":"2017-11-07T13:41:21","slug":"drive-api-v3%e3%81%a7%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%a2%e3%83%83%e3%83%97%e3%83%ad%e3%83%bc%e3%83%89%e3%81%8c%e3%81%a7%e3%81%8d%e3%81%aa%e3%81%84%e3%81%a8%e3%81%8d%e3%81%af","status":"publish","type":"post","link":"https:\/\/embedded-property.net\/?p=3962","title":{"rendered":"Drive API V3\u3067\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u304c\u3067\u304d\u306a\u3044\u3068\u304d\u306f"},"content":{"rendered":"<p>\u975e\u5e38\u306b\u4fbf\u5229\u306aGoogle Drive\u3002<\/p>\n<p>\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u3082\u5229\u7528\u3082\u7c21\u5358\u3067\u3059\u3057\u3001Drive API \u3092\u5229\u7528\u3059\u308c\u3070\u6163\u308c\u89aa\u3057\u3093\u3060.NET\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u304c\u3067\u304d\u3061\u3083\u3044\u307e\u3059\u3002<\/p>\n<p>\u5c0e\u5165\u65b9\u6cd5\u304b\u3089\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5145\u5b9f\u3057\u3066\u304a\u308a\u3001\u8a8d\u8a3c\u3084\u30d5\u30a1\u30a4\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u306f\u3059\u3050\u306b\u59cb\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3057\u304b\u3057\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u3044\u304d\u306a\u308a\u8e93\u3044\u3066\u3057\u307e\u3057\u307e\u3057\u305f\u3002<br \/>\n<!--more--><\/p>\n<p>\u6307\u5b9a\u30d5\u30a9\u30eb\u30c0\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u306b\u3042\u305f\u308a\u3001\u3053\u3061\u3089\u306e\u30da\u30fc\u30b8\u3092\u53c2\u7167\u3057\u307e\u3057\u305f\u3002<br \/>\n<a href=\"https:\/\/developers.google.com\/drive\/v3\/web\/folder\" target=\"_blank\" rel=\"noopener\">https:\/\/developers.google.com\/drive\/v3\/web\/folder<\/a><\/p>\n<p>\u30ed\u30fc\u30ab\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3092\u884c\u3044\u305f\u3044\u306e\u3067\u300cInserting a file in a folder\u300d\u306e.NET\u306e\u9805\u76ee\u307e\u3067\u79fb\u52d5\u3057\u307e\u3059\u3002\u30b3\u30fc\u30c9\u3092\u305d\u306e\u307e\u307e\u5f15\u7528\u3059\u308b\u3068\uff0e\uff0e\uff0e<\/p>\n<pre class=\"lang:c# decode:true \">var folderId = \"0BwwA4oUTeiV1TGRPeTVjaWRDY1E\";\r\nvar fileMetadata = new File()\r\n{\r\n    Name = \"photo.jpg\",\r\n    Parents = new List&lt;string&gt;\r\n    {\r\n        folderId\r\n    }\r\n};\r\nFilesResource.CreateMediaUpload request;\r\nusing (var stream = new System.IO.FileStream(\"files\/photo.jpg\",\r\n    System.IO.FileMode.Open))\r\n{\r\n    request = driveService.Files.Create(\r\n        fileMetadata, stream, \"image\/jpeg\");\r\n    request.Fields = \"id\";\r\n    request.Upload();\r\n}\r\nvar file = request.ResponseBody;\r\nConsole.WriteLine(\"File ID: \" + file.Id);<\/pre>\n<p>\u51fa\u5178\uff1ahttps:\/\/developers.google.com\/drive\/v3\/web\/folder<br \/>\n(folderId\u306f\u30c9\u30e9\u30a4\u30d6ID\u3068\u3057\u3066\u3001\u307e\u305f\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5bfe\u8c61\u306e\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\u3082\u9069\u5b9c\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044)<\/p>\n<p>17\u884c\u76ee\u306eUpload()\u30e1\u30bd\u30c3\u30c9\u3067\u5b9f\u884c\u3059\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u623b\u308a\u5024\u3067Exception\u304c\u767a\u751f\u3002Message\u306f<\/p>\n<pre class=\"lang:xhtml decode:true \">\"Google.Apis.Requests.RequestError\\r\\nInsufficient Permission [403]\\r\\nErrors [\\r\\n\\tMessage[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]\\r\\n]\\r\\n\"<\/pre>\n<p>\u306e\u3088\u3046\u306a\u982d\u306e\u75db\u3044\u611f\u3058\u3002<\/p>\n<p>domain\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3088\u3046\u3068\u3057\u3066\u3044\u305f\u306e\u3067\u3001\u7279\u5225\u306a\u6a29\u9650\u304c\u3044\u308b\u3093\u3060\u308d\u3046\u304b&#8230;\u3068\u6570\u65e5\u982d\u3092\u637b\u308a\u7d9a\u3051\u307e\u3057\u305f\u304c\u3001\u7b54\u3048\u306f\u3072\u3087\u3093\u306a\u6240\u304b\u3089\u964d\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<h2>scope\u3092\u78ba\u8a8d\u3057\u3088\u3046<\/h2>\n<p>stackoverflow\u306a\u3069\u3092\u8aad\u307f\u6f01\u308b\u3068\u3001scope\u306b\u3007\u3007\u3092\u8ffd\u52a0\u3057\u308d!\u306e\u3088\u3046\u306a\u3053\u3068\u304c\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u5f8c\u4ed8\u3051\u306escope\u306a\u3069\u3069\u3046\u3084\u3063\u3066\u8ffd\u52a0\u3057\u3066\u3044\u3044\u306e\u304b\u7686\u76ee\u898b\u5f53\u304c\u3064\u304d\u307e\u305b\u3093\u3002<br \/>\n\u305d\u3046\u306a\u3093\u3067\u3059\u3002\u5f8c\u4ed8\u3051\u3067\u304d\u306a\u3044\u3093\u3067\u3059\u3088\u3002(\u65b9\u6cd5\u304c\u306a\u3044\u3068\u3044\u3046\u308f\u3051\u3067\u306f\u306a\u304f\u3001\u77e5\u3089\u306a\u3044\u3060\u3051\u3067\u3059)<\/p>\n<p>\u3064\u307e\u308a\u3001\u8a8d\u8a3c\u306e\u6bb5\u968e\u3067\u5f97\u305fscope\u304c\u305d\u3082\u305d\u3082\u9593\u9055\u3063\u3066\u3044\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u3044\u3046\u7591\u554f\u306b\u9054\u3057\u305f\u306e\u3067\u3059\u3002<\/p>\n<p>\u30bd\u30fc\u30b9\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:c# decode:true \">using Google.Apis.Auth.OAuth2;\r\nusing Google.Apis.Drive.v3;\r\nusing Google.Apis.Drive.v3.Data;\r\nusing Google.Apis.Services;\r\nusing Google.Apis.Util.Store;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace DriveQuickstart\r\n{\r\n    class Program\r\n    {\r\n        \/\/ If modifying these scopes, delete your previously saved credentials\r\n        \/\/ at ~\/.credentials\/drive-dotnet-quickstart.json\r\n        static string[] Scopes = {  DriveService.Scope.DriveReadonly };\r\n        static string ApplicationName = \"Drive API .NET Quickstart\";\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            UserCredential credential;\r\n\r\n            using (var stream =\r\n                new FileStream(\"client_secret.json\", FileMode.Open, FileAccess.Read))\r\n            {\r\n                string credPath = System.Environment.GetFolderPath(\r\n                    System.Environment.SpecialFolder.Personal);\r\n                credPath = Path.Combine(credPath, \".credentials\/drive-dotnet-quickstart.json\");\r\n\r\n                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(\r\n                    GoogleClientSecrets.Load(stream).Secrets,\r\n                    Scopes,\r\n                    \"user\",\r\n                    CancellationToken.None,\r\n                    new FileDataStore(credPath, true)).Result;\r\n                Console.WriteLine(\"Credential file saved to: \" + credPath);\r\n            }\r\n\r\n            \/\/ Create Drive API service.\r\n            var service = new DriveService(new BaseClientService.Initializer()\r\n                {\r\n                    HttpClientInitializer = credential,\r\n                    ApplicationName = ApplicationName,\r\n                });\r\n\r\n            \/\/ Define parameters of request.\r\n            FilesResource.ListRequest listRequest = service.Files.List();\r\n            listRequest.PageSize = 10;\r\n            listRequest.Fields = \"nextPageToken, files(id, name)\";\r\n\r\n            \/\/ List files.\r\n            IList&lt;Google.Apis.Drive.v3.Data.File&gt; files = listRequest.Execute()\r\n                .Files;\r\n            Console.WriteLine(\"Files:\");\r\n            if (files != null &amp;&amp; files.Count &gt; 0)\r\n            {\r\n                foreach (var file in files)\r\n                {\r\n                    Console.WriteLine(\"{0} ({1})\", file.Name, file.Id);\r\n                }\r\n            }\r\n            else\r\n            {\r\n                Console.WriteLine(\"No files found.\");\r\n            }\r\n            Console.Read();\r\n\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u51fa\u5178\uff1ahttps:\/\/developers.google.com\/drive\/v3\/web\/quickstart\/dotnet<\/p>\n<p>\u3044\u307e\u3057\u305f&#8230;20\u884c\u76ee\u3002\u3057\u304b\u3068<\/p>\n<pre class=\"start-line:20 lang:c# decode:true \" >static string[] Scopes = {  DriveService.Scope.DriveReadonly };<\/pre>\n<p>\u3068\u3042\u308b\u3058\u3083\u306a\u3044\u3067\u3059\u304b\u3002<br \/>\n\u3053\u3061\u3089\u3092<\/p>\n<pre class=\"start-line:20 lang:c# decode:true \" >static string[] Scopes = {  DriveService.Scope.DriveFile };<\/pre>\n<p>\u3068\u76f4\u3057\u3066\u3001\u518d\u8a8d\u8a3c\u3059\u308c\u3070\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u304c\u53ef\u80fd\u306b\u306a\u3063\u305f\u3068\u3055\u3002<\/p>\n<blockquote><p>\u8a8d\u8a3c\u30d5\u30a1\u30a4\u30eb\u306f\u30b5\u30f3\u30d7\u30eb\u306e\u307e\u307e\u3060\u3068<br \/>\nC:\\Users\\(\u30e6\u30fc\u30b6\u30fc\u540d)\\Documents\\.credentials\\drive-dotnet-quickstart.json<br \/>\n\u306b\u3042\u308a\u307e\u3059\u3002json\u30d5\u30a1\u30a4\u30eb\u3092\u524a\u9664\u3057\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8(exe\u30d5\u30a1\u30a4\u30eb)\u3092\u5b9f\u884c\u3059\u308b\u3068\u518d\u8a8d\u8a3c\u3055\u308c\u307e\u3059<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u5e38\u306b\u4fbf\u5229\u306aGoogle Drive\u3002 \u30d6\u30e9\u30a6\u30b6\u304b\u3089\u3082\u5229\u7528\u3082\u7c21\u5358\u3067\u3059\u3057\u3001Drive API \u3092\u5229\u7528\u3059\u308c\u3070\u6163\u308c\u89aa\u3057\u3093\u3060.NET\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u304c\u3067\u304d\u3061\u3083\u3044\u307e\u3059\u3002 \u5c0e\u5165\u65b9\u6cd5\u304b\u3089\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5145\u5b9f\u3057\u3066\u304a\u308a\u3001\u8a8d\u8a3c\u3084<\/p>\n","protected":false},"author":1,"featured_media":3976,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jin_ogp_image_url":"","_jin_last_featured_id":0,"footnotes":"","_wp_rev_ctl_limit":""},"categories":[1775,1685,55],"tags":[1776,1777,1778],"class_list":["post-3962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-drive-api","category-google-service","category-it-web-service","tag-net","tag-drive-api-v3","tag-scope"],"acf":[],"_links":{"self":[{"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/posts\/3962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-property.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3962"}],"version-history":[{"count":2,"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/posts\/3962\/revisions"}],"predecessor-version":[{"id":3975,"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/posts\/3962\/revisions\/3975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-property.net\/index.php?rest_route=\/wp\/v2\/media\/3976"}],"wp:attachment":[{"href":"https:\/\/embedded-property.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-property.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-property.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}