{"id":5317,"date":"2023-10-05T13:52:01","date_gmt":"2023-10-05T04:52:01","guid":{"rendered":"https:\/\/www.lining-moon.casa\/andplus.co.jp\/?p=5317"},"modified":"2026-06-03T23:41:41","modified_gmt":"2026-06-03T14:41:41","slug":"post-5317","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/post-5317\/","title":{"rendered":"Google Drive API\u3092\u4f7f\u7528\u3057\u3066\u3001Colab\u304b\u3089Drive\u306e\u30d5\u30a1\u30a4\u30eb\u3084\u30d5\u30a9\u30eb\u30c0\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Google Drive API\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001Google Colab\u304b\u3089Drive\u306e\u30d5\u30a1\u30a4\u30eb\u3084\u30d5\u30a9\u30eb\u30c0\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u305d\u306e\u624b\u9806\u3092\u8a73\u3057\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Google Drive API\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5<\/h2>\n<h3><br \/>\n1. <a href=\"https:\/\/console.cloud.google.com\" target=\"_blank\" rel=\"noopener noreferrer\">Google Cloud Console<\/a>\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3001\u65b0\u3057\u3044\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210<\/h3>\n<p><br \/>\nGoogle Cloud Console\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<br \/>\n\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30c9\u30ed\u30c3\u30d7\u30c0\u30a6\u30f3\u30e1\u30cb\u30e5\u30fc\u3092\u30af\u30ea\u30c3\u30af\u3057\u3001\u300c\u65b0\u3057\u3044\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u3092\u5165\u529b\u3057\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<h3><br \/>\n2. Google Drive API\u3092\u6709\u52b9\u306b\u3059\u308b<\/h3>\n<p><br \/>\n\u5de6\u5074\u306e\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u30e1\u30cb\u30e5\u30fc\u304b\u3089\u300cAPI\u3068\u30b5\u30fc\u30d3\u30b9\u300d&gt;\u300c\u30e9\u30a4\u30d6\u30e9\u30ea\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n\u300cGoogle Drive API\u300d\u3092\u691c\u7d22\u3057\u3001\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n\u300c\u6709\u52b9\u306b\u3059\u308b\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<\/p>\n<h3><br \/>\n3. \u8a8d\u8a3c\u60c5\u5831\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n<p><br \/>\n\u5de6\u5074\u306e\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u30e1\u30cb\u30e5\u30fc\u304b\u3089\u300cAPI\u3068\u30b5\u30fc\u30d3\u30b9\u300d&gt;\u300c\u8a8d\u8a3c\u60c5\u5831\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n\u300c\u8a8d\u8a3c\u60c5\u5831\u3092\u4f5c\u6210\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3001OAuth 2.0 \u30af\u30e9\u30a4\u30a2\u30f3\u30c8 ID \u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30bf\u30a4\u30d7\u3068\u3057\u3066\u300c\u305d\u306e\u4ed6\u300d\u3092\u9078\u629e\u3057\u3001\u8a8d\u8a3c\u60c5\u5831\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<h3><br \/>\n4. JSON\u30ad\u30fc\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/h3>\n<p><br \/>\n\u300c\u8a8d\u8a3c\u60c5\u5831\u300d\u30da\u30fc\u30b8\u306b\u623b\u308a\u3001\u4f5c\u6210\u3057\u305f\u30b5\u30fc\u30d3\u30b9 \u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<br \/>\n\u300c\u30ad\u30fc\u300d\u30bf\u30d6\u3092\u30af\u30ea\u30c3\u30af\u3057\u3001\u300c\u30ad\u30fc\u3092\u8ffd\u52a0\u300d&gt;\u300cJSON\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\nJSON\u30ad\u30fc\u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u30ad\u30fc\u306f\u3001\u5f8c\u3067Colab\u304b\u3089Drive\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u969b\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<h3><br \/>\n5. Colab\u3067Google Drive API\u3092\u4f7f\u7528\u3059\u308b<\/h3>\n<p><br \/>\nGoogle Drive\u4e0a\u3067\u300cOAuth\u300d\u3068\u3044\u3046\u540d\u524d\u306e\u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u6210\u3057\u3001\u305d\u306e\u4e2d\u306b<br \/>\n\u5148\u307b\u3069\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305fJSON\u30ad\u30fc\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<p>Google Colab\u3067\u3053\u306eJSON\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u7528\u3059\u308b\u969b\u306b\u306f\u3001Google Drive\u3092\u30de\u30a6\u30f3\u30c8\u3057\u3066\u3001\u8a72\u5f53\u306e\u30d5\u30a9\u30eb\u30c0\u304b\u3089\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Service Account\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5<\/h2>\n<p><br \/>\nOAuth 2.0 \u30af\u30e9\u30a4\u30a2\u30f3\u30c8 ID \u306e\u8a8d\u8a3c\u60c5\u5831\u30d5\u30a1\u30a4\u30eb\u306b\u306f\u3001<br \/>\nrefresh_token\u3001client_id\u3001\u304a\u3088\u3073client_secret\u306e\u60c5\u5831\u304c\u4e0d\u8db3\u3057\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<br \/>\n\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3001Service Account\u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>1. <a href=\"https:\/\/console.cloud.google.com\" target=\"_blank\" rel=\"noopener noreferrer\">Google Cloud Console<\/a>\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3001\u5148\u307b\u3069\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n2. \u5de6\u5074\u306e\u30ca\u30d3\u30b2\u30fc\u30b7\u30e7\u30f3\u30e1\u30cb\u30e5\u30fc\u304b\u3089\u300cIAM &amp; \u7ba1\u7406\u300d&gt;\u300c\u30b5\u30fc\u30d3\u30b9 \u30a2\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n3. \u300c\u30b5\u30fc\u30d3\u30b9 \u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u4f5c\u6210\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<br \/>\n4. \u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u540d\u524d\u3068\u8aac\u660e\u3092\u5165\u529b\u3057\u3001\u6b21\u306b\u9032\u307f\u307e\u3059\u3002<br \/>\n5. \u5fc5\u8981\u306a\u5f79\u5272\u3092\u9078\u629e\u305b\u305a\u306b\u6b21\u306b\u9032\u307f\u307e\u3059\u3002<br \/>\n6. \u300c\u5b8c\u4e86\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n7. \u4f5c\u6210\u3057\u305f\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u884c\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u8a73\u7d30\u30da\u30fc\u30b8\u306b\u79fb\u52d5\u3057\u307e\u3059\u3002<br \/>\n8. \u300c\u30ad\u30fc\u300d\u30bf\u30d6\u3092\u30af\u30ea\u30c3\u30af\u3057\u3001\u300c\u30ad\u30fc\u3092\u8ffd\u52a0\u300d&gt;\u300cJSON\u300d\u3092\u9078\u629e\u3057\u3066\u3001JSON\u30ad\u30fc\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<br \/>\n9. \u3053\u306eJSON\u30ad\u30fc\u3092Google Drive\u4e0a\u306e\u300cOAuth\u300d\u30d5\u30a9\u30eb\u30c0\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Service Account\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067Google Drive\u306e\u30d5\u30a1\u30a4\u30eb\u3084\u30d5\u30a9\u30eb\u30c0\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u305b\u3093\u3002<br \/>\nService Account\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u306f\u3001project-************.json\u306e\u4e2d\u306b\"client_email\"\u3068\u3057\u3066\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n\u7d9a\u3044\u3066\u3001\u3053\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3092\u4f7f\u7528\u3057\u3066\u3001Google Drive\u306e\u5171\u6709\u8a2d\u5b9a\u3067\u30a2\u30af\u30bb\u30b9\u6a29\u9650\u3092\u4ed8\u4e0e\u3059\u308b\u624b\u9806\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>1. Google Drive\u3092\u958b\u304d\u3001\u30a2\u30af\u30bb\u30b9\u3092\u8a31\u53ef\u3057\u305f\u3044\u30d5\u30a9\u30eb\u30c0\u3084\u30d5\u30a1\u30a4\u30eb\u3092\u53f3\u30af\u30ea\u30c3\u30af\u3057\u307e\u3059\u3002<br \/>\n2. \u300c\u5171\u6709\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<br \/>\n3. \u300c\u30ea\u30f3\u30af\u3092\u77e5\u3063\u3066\u3044\u308b\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u4e0a\u306e\u8ab0\u3082\u304c\u95b2\u89a7\u3067\u304d\u307e\u3059\u300d\u306e\u90e8\u5206\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u3001\u5171\u6709\u8a2d\u5b9a\u3092\u5909\u66f4\u3057\u307e\u3059\u3002<br \/>\n4. \u300c\u30e6\u30fc\u30b6\u30fc\u3084\u30b0\u30eb\u30fc\u30d7\u3092\u8ffd\u52a0\u300d\u306einput\u90e8\u5206\u306b\u3001Service Account\u306eclient_email\uff08project-*****@project-*****.iam.gserviceaccount.com \u7b49\uff09\u3092\u5165\u529b\u3057\u307e\u3059\u3002<br \/>\n5. \u6a29\u9650\u3092\u300c\u7de8\u96c6\u8005\u300d\u3084\u300c\u95b2\u89a7\u8005\u300d\u306a\u3069\u3001\u9069\u5207\u306a\u3082\u306e\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br \/>\n6. \u5909\u66f4\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u306b\u3088\u308a\u3001Service Account\u306f\u6307\u5b9a\u3057\u305f\u30d5\u30a9\u30eb\u30c0\u3084\u30d5\u30a1\u30a4\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f8b\u3068\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001<br \/>\nGoogle Drive\u306e\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u30d5\u30a1\u30a4\u30eb\u3068\u30d5\u30a9\u30eb\u30c0\u3092\u4e00\u89a7\u8868\u793a\u3057\u307e\u3059\uff1a<\/p>\n<pre class=\"language-python\"><code>\n# Google Drive\u3092\u30de\u30a6\u30f3\u30c8\nfrom google.colab import drive\ndrive.mount('\/content\/drive')\n\nfrom google.oauth2.service_account import Credentials\nimport googleapiclient.discovery\n\n# JSON\u30ad\u30fc\u306e\u30d1\u30b9\u3092\u6307\u5b9a\ncredential_path = \"\/content\/drive\/MyDrive\/OAuth\/json_key.json\" #@param {type:\"string\"}\n\n# Service Account\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u4f7f\u7528\u3057\u3066Drive API\u306e\u30b5\u30fc\u30d3\u30b9\u3092\u4f5c\u6210\ncreds = Credentials.from_service_account_file(credential_path, scopes=[\"https:\/\/www.googleapis.com\/auth\/drive\"])\ndrive_service = googleapiclient.discovery.build('drive', 'v3', credentials=creds)\n\n# \u4f8b: Google Drive\u306e\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u30d5\u30a1\u30a4\u30eb\u3068\u30d5\u30a9\u30eb\u30c0\u3092\u4e00\u89a7\u8868\u793a\nresults = drive_service.files().list(pageSize=10, fields=\"nextPageToken, files(id, name)\").execute()\nitems = results.get('files', [])\nfor item in items:\n    print(f\"{item['name']} (folder_id: {item['id']})\")\n<\/code><\/pre>\n<p><br \/>\n\u3053\u306e\u624b\u9806\u306b\u5f93\u3048\u3070\u3001Google Colab\u304b\u3089Google Drive\u306e\u30d5\u30a1\u30a4\u30eb\u3084\u30d5\u30a9\u30eb\u30c0\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>","protected":false},"excerpt":{"rendered":"Google Drive API\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001Google Colab\u304b\u3089Drive\u306e\u30d5\u30a1\u30a4\u30eb\u3084\u30d5\u30a9\u30eb\u30c0\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u305d\u306e\u624b\u9806\u3092\u8a73\u3057\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002 &nbsp; Google Dr[...]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","footnotes":"","vk-ltc-link":"","vk-ltc-target":""},"categories":[957],"tags":[],"class_list":["post-5317","post","type-post","status-publish","format-standard","category-backend"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/5317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/comments?post=5317"}],"version-history":[{"count":1,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/5317\/revisions"}],"predecessor-version":[{"id":12640,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/5317\/revisions\/12640"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=5317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=5317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=5317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}