{"id":17374,"date":"2021-10-15T17:54:41","date_gmt":"2021-10-15T08:54:41","guid":{"rendered":"https:\/\/itport.cloud\/?p=17374"},"modified":"2021-10-15T17:54:42","modified_gmt":"2021-10-15T08:54:42","slug":"post-17374","status":"publish","type":"post","link":"https:\/\/itport.cloud\/?p=17374","title":{"rendered":"\uff08\u7b2c\uff13\u56de\uff09GmailAPI\u3092\u4f7f\u7528\u3057\u305f\u30e1\u30fc\u30eb\u306e\u81ea\u52d5\u4f5c\u6210"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"e79baee6aca1-1\">\u76ee\u6b21<\/h2>\n\n\n\n<p><a href=\"#A-1\">\u632f\u308a\u8fd4\u308a<\/a><br><a href=\"#A-2\"> \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4fee\u6b63 <\/a><br><a href=\"#A-3\">\u52d5\u4f5c\u78ba\u8a8d<\/a><br><a href=\"#A-4\">\u307e\u3068\u3081<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"A-1\"> \u632f\u308a\u8fd4\u308a <\/h2>\n\n\n\n<p>\u524d\u56de\u306f\u56fa\u5b9a\u30d1\u30b9\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30d1\u30b9\u30ef\u30fc\u30c9\u4ed8\u304d\u3067\u5727\u7e2e\u3057\u3001\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u6dfb\u4ed8\u3057\u305f\u4e0b\u66f8\u304d\u3068\u5727\u7e2e\u3059\u308b\u969b\u306b\u4f7f\u7528\u3057\u305f\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u8a18\u8f09\u3055\u308c\u305f\u4e0b\u66f8\u304d\u3092\u4f5c\u6210\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u884c\u3044\u307e\u3057\u305f\u3002 <br>\u4eca\u56de\u306f\u6c4e\u7528\u7684\u306b\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3067\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3067\u304d\u308b\u3088\u3046\u306b\u5909\u66f4\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"A-2\">\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4fee\u6b63<\/h2>\n\n\n\n<p>\u4e0b\u8a18\u306e\uff12\u70b9\u3092\u3067\u304d\u308b\u3088\u3046\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4fee\u6b63\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3067\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u5f15\u6570\u3067\u6e21\u3059<\/li><li>\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/li><\/ul>\n\n\n\n<p>\uff11\u3001import\u306e\u8ffd\u52a0\u3092\u3057\u307e\u3059\u3002<br>\u2192\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308b\u969b\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sys\n<\/pre><\/div>\n\n\n<p>\uff12\u3001\u30e1\u30fc\u30eb\u672c\u6587\u306e\u4f5c\u6210\u3092\u3057\u3066\u3044\u308b\u300ccreate_message_compress\u300d\u30e1\u30bd\u30c3\u30c9\u306b\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# \u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3057\u305f\u969b\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u53d6\u5f97\u3000\u30fb\u30fb\u30fb\u3000\u203b\uff11\nfile_paths = sys.argv&#x5B;1:]\n\n# \u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3057\u305f\u969b\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u306e\u533a\u5207\u308a\u6587\u5b57\u3092\u300c\/\u300d\u306b\u5909\u66f4\u3000\u30fb\u30fb\u30fb\u3000\u203b\uff12\nfor index, value in enumerate(file_paths):\n    # \u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u306e\u533a\u5207\u308a\u6587\u5b57\u3092\u5909\u66f4\n    file_paths&#x5B;index] = value.replace(\"\\\\\", \"\/\")\n    # \u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u968e\u5c64\u3092\u8ffd\u52a0\u3000\u30fb\u30fb\u30fb\u3000\u203b\uff13\n    file_depth.append(\"\")\n\n<\/pre><\/div>\n\n\n<p>\u203b\uff11\uff1a\u300csys.argv[1:]\u300d\u3067\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u304c\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u59cb\u307e\u308a\u304c\u3001\u300c\uff11\u300d\u3068\u306a\u308b\u70ba<br>\u3000\u3000\u3000\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br>\u3000\u3000\u3000\u3061\u306a\u307f\u306b\u300c\uff10\u300d\u306b\u306f\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u304c\u5165\u3063\u3066\u304a\u308a\u307e\u3059\u3002<br>\u203b\uff12\uff1a\u5f15\u6570\u304b\u3089\u30d1\u30b9\u3092\u53d6\u5f97\u3059\u308b\u3068\u533a\u5207\u308a\u6587\u5b57\u304c\u300c\\\u300d\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br>\u3000\u3000\u3000 Python\u4e0a\u3067\u300c\\\u300d\u306f\u30a8\u30b9\u30b1\u30fc\u30d7\u6587\u5b57\u3068\u306a\u3063\u3066\u304a\u308a\u3001\u305d\u306e\u307e\u307e\u3060\u3068\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3059\u308b\u969b\u306b\u30a8\u30e9\u30fc\u3068\u306a\u308b\u70ba<br>\u3000\u3000\u3000\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br>\u203b\uff13\uff1a\u30d5\u30a1\u30a4\u30eb\u3054\u3068\u306b\u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u968e\u5c64\u3092\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u306b\u306f\u30b3\u30fc\u30c9\u3092\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<p> \uff13\u3001\u5727\u7e2e\u3059\u308b\u969b\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u5909\u66f4\u3057\u307e\u3059\u3002<br>\u300ccompress\u300d\u2192\u300ccompress_multiple\u300d<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npyminizip.compress_multiple(file_paths, file_depth, path, password, 1)  \n<\/pre><\/div>\n\n\n<p>\u5f15\u6570\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3067\u3059\u3002<br> \u300ccompress\u300d\u306e\u5f15\u6570\u3068\u307b\u3068\u3093\u3069\u540c\u3058\u3067\u3059\u304c\u3001\u7b2c\uff11\u3068\u7b2c\uff12\u304c\u914d\u5217\u3082\u3057\u304f\u306f\u30ea\u30b9\u30c8\u3068\u306a\u308b\u70b9\u306b\u6ce8\u610f\u3057\u304f\u3060\u3055\u3044\u3002 <br>\u3000\u7b2c\uff11\uff1a\u5727\u7e2e\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u914d\u5217\u3082\u3057\u304f\u306f\u30ea\u30b9\u30c8\u3067\u6307\u5b9a\uff08\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u542b\u3081\u308b\uff09<br>\u3000\u7b2c\uff12\uff1a\u5727\u7e2e\u30d5\u30a9\u30eb\u30c0\u5185\u306e\u968e\u5c64\u3092\u914d\u5217\u3082\u3057\u304f\u306f\u30ea\u30b9\u30c8\u3067\u6307\u5b9a<br>\u3000\u7b2c\uff13\uff1a \u51fa\u529b\u3059\u308b\u969b\u306e\u30d1\u30b9\u3092\u6307\u5b9a \uff08\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u542b\u3081\u308b\uff09 <br>\u3000\u7b2c\uff14\uff1a \u5727\u7e2e\u3059\u308b\u969b\u306e\u30d1\u30b9\u30ef\u30fc\u30c9<br>\u3000\u7b2c\uff15\uff1a0\uff5e9\u3067\u5727\u7e2e\u7387\u3092\u6307\u5b9a\u3000<br>\u21920\u306f\u7121\u5727\u7e2e\u3067\u6570\u5024\u304c\u5927\u304d\u304f\u306a\u308c\u3070\u306a\u308b\u307b\u3069\u306b\u5727\u7e2e\u7387\u304c\u9ad8\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\uff14\u3001\u3053\u308c\u307e\u3067\u306f\u30b3\u30f3\u30bd\u30fc\u30eb\u304c\u81ea\u52d5\u3067\u9589\u3058\u3066\u3057\u307e\u3044\u3001\u6b63\u5e38\u7d42\u4e86\u3057\u305f\u304b\u7570\u5e38\u7d42\u4e86\u3057\u305f\u304b\u304c\u308f\u304b\u3089\u306a\u3044\u72b6\u614b\u3067\u3057\u305f\u306e\u3067\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<br>\u30e1\u30a4\u30f3\u306e\u95a2\u6570\u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif __name__ == '__main__':\n    try:\n        main()\n\n        # \u6b63\u5e38\u7d42\u4e86\u306e\u969b\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\n        print('\\n**********\u6b63\u5e38\u7d42\u4e86**********')\n\n        os.system('PAUSE')\u3000\u30fb\u30fb\u30fb\u3000\u203b\uff11\n\n    except Exception as e:\u3000\u3000\u30fb\u30fb\u30fb\u3000\u203b\uff12\n\n        # \u7570\u5e38\u7d42\u4e86\u306e\u969b\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\n        print('**********\u7570\u5e38\u7d42\u4e86**********\\n')\n        # \u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u51fa\u529b\n        print(e)      \n\n        os.system('PAUSE')\u3000\u30fb\u30fb\u30fb\u3000\u203b\uff11\n<\/pre><\/div>\n\n\n<p>\u203b\uff11\uff1a\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u5b9f\u884c\u5f8c\u306b\u30b3\u30f3\u30bd\u30fc\u30eb\u304c\u9589\u3058\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3059\u3002<br>\u203b\uff12\uff1a\u4eca\u56de\u306f\u300cExcepthion\u300d\u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u304c\u3001\u7279\u5b9a\u306e\u30a8\u30e9\u30fc\u306e\u307f\u51e6\u7406\u3092\u5909\u66f4\u3084\u7279\u5b9a\u306e\u30a8\u30e9\u30fc\u306e\u307f\u30a8\u30e9\u30fc\u3092\u51fa\u529b\u3059\u308b\u969b\u306b\u306f\u300cExcepthion\u300d\u306e\u5909\u66f4\u3084\u8ffd\u52a0\u3092\u3057\u3066\u304f\u3060\u3055\u3044\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"A-3\">\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n\n\n\n<p>\uff11\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u8907\u6570\u9078\u629e\u3057\u3066\u300cquickstart.py\u300d\u306b\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"346\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-1024x346.png\" alt=\"\" class=\"wp-image-17384\" srcset=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-1024x346.png 1024w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-300x101.png 300w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-768x259.png 768w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-1536x519.png 1536w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-450x152.png 450w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c-900x304.png 900w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/\u7121\u984c.png 1909w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\uff12\u3001\u6b63\u5e38\u7d42\u4e86\u3057\u4e0b\u8a18\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"205\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15-1024x205.png\" alt=\"\" class=\"wp-image-17381\" srcset=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15-1024x205.png 1024w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15-300x60.png 300w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15-768x154.png 768w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15-450x90.png 450w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15-900x181.png 900w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/image-15.png 1077w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\uff13\u3001\u4e0b\u66f8\u304d\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"427\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-1024x427.png\" alt=\"\" class=\"wp-image-17385\" srcset=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-1024x427.png 1024w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-300x125.png 300w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-768x320.png 768w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-1536x640.png 1536w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-450x188.png 450w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1-900x375.png 900w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/1.png 1845w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\uff14\u3001\u300c\u624b\u9806\uff13\u300d\u306e\u4e0b\u66f8\u304d\u306b\u6dfb\u4ed8\u3055\u308c\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"313\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/2.png\" alt=\"\" class=\"wp-image-17386\" srcset=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/2.png 580w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/2-300x162.png 300w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/2-450x243.png 450w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/figure>\n\n\n\n<p>\uff15\u3001\u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u3092\u5c55\u958b\u3057\u307e\u3059\u3002<br>\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3059\u308b\u969b\u306b\u6307\u5b9a\u3057\u3066\u3044\u305f\u30d5\u30a1\u30a4\u30eb\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/4-1.png\" alt=\"\" class=\"wp-image-17389\" width=\"468\" height=\"200\"\/><\/figure>\n\n\n\n<p>\uff16\u3001\u53c2\u8003\u307e\u3067\u306b\u30a8\u30e9\u30fc\u3067\u505c\u6b62\u3057\u305f\u969b\u306b\u306f\u4e0b\u8a18\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br>\u8d64\u67a0\u304c\u30a8\u30e9\u30fc\u306e\u5185\u5bb9\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"180\" src=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/3-1.png\" alt=\"\" class=\"wp-image-17391\" srcset=\"https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/3-1.png 598w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/3-1-300x90.png 300w, https:\/\/itport.cloud\/wp-content\/uploads\/2021\/10\/3-1-450x135.png 450w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/figure>\n\n\n\n<p>\uff17\u3001\u30b3\u30fc\u30c9\u5168\u4f53\u306f\u4e0b\u8a18\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom __future__ import print_function\nimport pickle\nimport os.path\nfrom googleapiclient.discovery import build\nfrom google_auth_oauthlib.flow import InstalledAppFlow\nfrom google.auth.transport.requests import Request\n# \u30af\u30a4\u30c3\u30af\u30b9\u30bf\u30fc\u30c8\u306b\u8db3\u308a\u306a\u3044\u9805\u76ee\u306eimport\nimport base64\nfrom email.mime.text import MIMEText\nfrom apiclient import errors\n# \u4eca\u56de\u8ffd\u52a0\u3057\u305fimport\n# \u30e1\u30fc\u30eb\u306e\u672c\u6587\u306e\u5185\u5bb9\u3092\u683c\u7d0d\u3059\u308b\u30af\u30e9\u30b9\nfrom email.mime.multipart import MIMEMultipart\n# \u30e1\u30fc\u30eb\u306e\u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u30af\u30e9\u30b9\nfrom email.mime.application import MIMEApplication\n# \u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u304b\u3089\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u53d6\u5f97\nfrom os.path import basename\n# \u30d1\u30b9\u30ef\u30fc\u30c9\u4ed8\u304d\u5727\u7e2e\u3059\u308b\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\nimport pyminizip\n# \u30d1\u30b9\u30ef\u30fc\u30c9\u751f\u6210\u306e\u4e71\u6570\u751f\u6210\u306b\u4f7f\u7528\nimport random\nimport sys\nimport warnings\nwarnings.simplefilter(\"ignore\")\n\n# Gmail API\u306e\u30b9\u30b3\u30fc\u30d7\u3092\u8a2d\u5b9a\nSCOPES = &#x5B;'https:\/\/www.googleapis.com\/auth\/gmail.modify']\n\n# Gmail\u306e\u4e0b\u66f8\u304d\u4f5c\u6210 \ndef create_draft(service, user_id, message_body):\n  try:\n    message = {'message': message_body}\n    draft = service.users().drafts().create(userId=user_id, body=message).execute()\n\n    print ('Draft id: %s\\nDraft message: %s' % (draft&#x5B;'id'], draft&#x5B;'message']))\n\n    return draft\n  except errors.HttpError as error:\n    print ('\u30a8\u30e9\u30fc: %s' % error)\n    return None\n\n# \u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb\u306e\u30e1\u30fc\u30eb\u672c\u6587\u306e\u4f5c\u6210\ndef create_message_compress(subject, message_text, password):\n    # \u30e1\u30fc\u30eb\u306e\u5185\u5bb9\u3092\u683c\u7d0d\n    message = MIMEMultipart()    \n    message&#x5B;'subject'] = subject\n    message.attach(MIMEText(message_text))\n    \n    # \u30d5\u30a1\u30a4\u30eb\u3092\u6dfb\u4ed8\n    path = \"C:\/GmaiDraft\/test.zip\"\n\n    # \u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3057\u305f\u969b\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u53d6\u5f97\n    file_paths = sys.argv&#x5B;1:]\n\n    # \u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u30d1\u30b9\u306e\u6307\u5b9a\u3082file_paths\u3068\u540c\u69d8\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3067\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u70ba\u3001\u914d\u5217\u3092\u8ffd\u52a0\n    file_depth = &#x5B;]\n\n    # \u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3057\u305f\u969b\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u306e\u533a\u5207\u308a\u6587\u5b57\u3092\u300c\/\u300d\u306b\u5909\u66f4\n    for index, value in enumerate(file_paths):\n        # \u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u306e\u533a\u5207\u308a\u6587\u5b57\u3092\u5909\u66f4\n        file_paths&#x5B;index] = value.replace(\"\\\\\", \"\/\")\n        # \u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u968e\u5c64\u3092\u8ffd\u52a0\n        file_depth.append(\"\")\n\n    pyminizip.compress_multiple(file_paths, file_depth, path, password, 1)    \n\n    with open(path, \"rb\") as f:\n        part = MIMEApplication(\n            f.read(),\n            Name=basename(path)\n        )\n    part&#x5B;'Content-Disposition'] = 'attachment; filename=\"%s\"' % basename(path)\n    message.attach(part)\n\n    encode_message = base64.urlsafe_b64encode(message.as_bytes())\n    return {'raw': encode_message.decode()}\n\n# \u30d1\u30b9\u30ef\u30fc\u30c9\u8a18\u8f09\u306e\u30e1\u30fc\u30eb\u672c\u6587\u306e\u4f5c\u6210\ndef create_message_password(subject, password):\n    # \u30e1\u30fc\u30eb\u306e\u5185\u5bb9\u3092\u683c\u7d0d\n    message = MIMEMultipart()    \n    message&#x5B;'subject'] = subject\n    message.attach(MIMEText(password))\n    \n    encode_message = base64.urlsafe_b64encode(message.as_bytes())\n    return {'raw': encode_message.decode()}\n\n# \u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u4f5c\u6210\ndef create_password():\n    # \u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u6841\u6570\n    passwordLength = 8\n \n    # \u30d1\u30b9\u30ef\u30fc\u30c9\n    password = ''\n \n    #  \u30d1\u30b9\u30ef\u30fc\u30c9\u306b\u4f7f\u3046\u6587\u5b57\u5217\n    passwordPattern = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'\n \n    # \u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u751f\u6210\u30eb\u30fc\u30d7\n    for i in range(0, passwordLength):\n        password = password + random.sample(passwordPattern, 1)&#x5B;0]\n\n    return password\n\n# \u30e1\u30a4\u30f3\u306e\u51e6\u7406\ndef main():\n    # Google\u8a8d\u8a3c\u306e\u51e6\u7406\n    creds = None\n    if os.path.exists('token.pickle'):\n        with open('token.pickle', 'rb') as token:\n            creds = pickle.load(token)\n    if not creds or not creds.valid:\n        if creds and creds.expired and creds.refresh_token:\n            creds.refresh(Request())\n        else:\n            flow = InstalledAppFlow.from_client_secrets_file(\n                'credentials.json', SCOPES)\n            creds = flow.run_local_server()\n        with open('token.pickle', 'wb') as token:\n            pickle.dump(creds, token)\n\n    # GmailAPI\u3092\u5b9f\u884c\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u4f5c\u6210\n    service = build('gmail', 'v1', credentials=creds)\n\n    # \u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u4f5c\u6210\n    password = create_password()\n\n    # \u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb\u4ed8\u304d\u4e0b\u66f8\u304d\u30e1\u30fc\u30eb\u306e\u4f5c\u6210\n    subject = '\u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb\u30e1\u30fc\u30eb'\n    message_text = '\u30d5\u30a1\u30a4\u30eb\u3092\u6dfb\u4ed8\u3057\u3066\u3044\u307e\u3059'\n\n    message = create_message_compress(subject, message_text, password)\n\n    # \u4e0b\u66f8\u304d\u4f5c\u6210\n    create_draft(service, 'me', message)\n\n    # \u30d1\u30b9\u30ef\u30fc\u30c9\u8a18\u8f09\u3057\u305f\u4e0b\u66f8\u304d\u30e1\u30fc\u30eb\u306e\u4f5c\u6210\n    subject = '\u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb\u30e1\u30fc\u30eb'\n\n    message = create_message_password(subject, password)\n    \n    # \u4e0b\u66f8\u304d\u4f5c\u6210\n    create_draft(service, 'me', message)\n\nif __name__ == '__main__':\n    try:\n        main()\n\n        # \u6b63\u5e38\u7d42\u4e86\u306e\u969b\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\n        print('\\n**********\u6b63\u5e38\u7d42\u4e86**********')\n        os.system('PAUSE')\n\n    except Exception as e:\n\n        # \u7570\u5e38\u7d42\u4e86\u306e\u969b\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\n        print('**********\u7570\u5e38\u7d42\u4e86**********\\n')\n        print(e)      \n        os.system('PAUSE')\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"A-4\">\u307e\u3068\u3081 <\/h2>\n\n\n\n<p>\u4eca\u56de\u306f\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u306b\u3066\u4efb\u610f\u306e\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3057Gmail\u306e\u4e0b\u66f8\u304d\u306b\u6dfb\u4ed8\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3057\u305f\u3002<br>\u672c\u8a18\u4e8b\u306f\u4eca\u56de\u3067\u7d42\u4e86\u3068\u306a\u308a\u307e\u3059\u3002<br>\u4eca\u56de\u521d\u3081\u3066\u300cPython\u300d\u3092\u5229\u7528\u3057\u3001\u3046\u307e\u304f\u52d5\u4f5c\u305b\u305a\u82e6\u52b4\u306a\u3069\u3082\u3042\u308a\u307e\u3057\u305f\u304c\u3001\u3053\u308c\u3092\u6a5f\u306b\u300cPython\u300d\u3092\u4f7f\u7528\u3057\u305f\u8a18\u4e8b\u3092\u4eca\u5f8c\u3082\u66f8\u3044\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u76ee\u6b21 \u632f\u308a\u8fd4\u308a \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4fee\u6b63 \u52d5\u4f5c\u78ba\u8a8d\u307e\u3068\u3081 \u632f\u308a\u8fd4\u308a \u524d\u56de\u306f\u56fa\u5b9a\u30d1\u30b9\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30d1\u30b9\u30ef\u30fc\u30c9\u4ed8\u304d\u3067\u5727\u7e2e\u3057\u3001\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u6dfb\u4ed8\u3057\u305f\u4e0b\u66f8\u304d\u3068\u5727\u7e2e\u3059\u308b\u969b\u306b\u4f7f\u7528\u3057\u305f\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u8a18\u8f09\u3055\u308c\u305f\u4e0b\u66f8\u304d\u3092\u4f5c\u6210\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u884c\u3044\u307e\u3057\u305f\u3002 \u4eca\u56de\u306f\u6c4e\u7528\u7684\u306b\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3067\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3067\u304d\u308b\u3088\u3046\u306b\u5909\u66f4\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4fee\u6b63 \u4e0b\u8a18\u306e\uff12\u70b9\u3092\u3067\u304d\u308b\u3088\u3046\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4fee\u6b63\u3057\u3066\u3044\u304d\u307e\u3059\u3002 \u30c9\u30e9\u30c3\u30b0\u30a2\u30f3\u30c9\u30c9\u30ed\u30c3\u30d7\u3067\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u5f15\u6570\u3067\u6e21\u3059\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b \uff11\u3001import\u306e\u8ffd\u52a0\u3092\u3057\u307e\u3059\u3002\u2192\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308b\u969b\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002 import sys \uff12\u3001\u30e1\u30fc\u30eb\u672c\u6587\u306e\u4f5c\u6210\u3092\u3057\u3066\u3044\u308b\u300ccreate_mes&hellip;","protected":false},"author":1,"featured_media":17402,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105,106],"tags":[104],"class_list":{"0":"post-17374","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-gmail","8":"category-python","9":"tag-season34"},"_links":{"self":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts\/17374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17374"}],"version-history":[{"count":17,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts\/17374\/revisions"}],"predecessor-version":[{"id":17430,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/posts\/17374\/revisions\/17430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=\/wp\/v2\/media\/17402"}],"wp:attachment":[{"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itport.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}