{"id":1266,"date":"2020-04-14T15:08:08","date_gmt":"2020-04-14T06:08:08","guid":{"rendered":"https:\/\/ts-engine.net\/?p=1266"},"modified":"2020-04-14T15:08:08","modified_gmt":"2020-04-14T06:08:08","slug":"google-photo-api%e3%81%a7%e3%82%a2%e3%83%ab%e3%83%90%e3%83%a0%e3%81%ae%e4%bd%9c%e6%88%90%e3%81%a8%e4%bd%9c%e6%88%90%e3%81%97%e3%81%9f%e3%82%a2%e3%83%ab%e3%83%90%e3%83%a0%e3%81%ab%e5%86%99%e7%9c%9f","status":"publish","type":"post","link":"http:\/\/ts-engine.net\/?p=1266","title":{"rendered":"Google photo api\u3067\u30a2\u30eb\u30d0\u30e0\u306e\u4f5c\u6210\u3068\u4f5c\u6210\u3057\u305f\u30a2\u30eb\u30d0\u30e0\u306b\u5199\u771f\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b"},"content":{"rendered":"<p>\n<a href=\"http:\/\/ts-engine.net\/wp\/archives\/1262\">\u524d\u56de\u306e\u8a18\u4e8b<\/a><br \/>\n\u3067google photo api\u306e\u8a8d\u8a3c\u65b9\u6cd5\u3092\u307e\u3068\u3081\u305f\u3002<br \/>\n\u3053\u3053\u3067\u306f\u8a8d\u8a3c\u5f8c\u306b\u30a2\u30eb\u30d0\u30e0\u4f5c\u6210\u3001\u30a2\u30eb\u30d0\u30e0\u53d6\u5f97\u3001\u30a2\u30eb\u30d0\u30e0\u3078\u306e\u5199\u771f\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u65b9\u6cd5\u306e\u305f\u3081\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u305f\u306e\u3067\u4ee5\u4e0b\u306b\u307e\u3068\u3081\u308b\u3002\n<\/p>\n<p><!--more--><\/p>\n<h2>\ngoogle photo api\u30a2\u30eb\u30d0\u30e0\u53d6\u5f97<br \/>\n<\/h2>\n<p>\n<a href=\"http:\/\/ts-engine.net\/wp\/archives\/1262\">\u524d\u56de\u306e\u8a18\u4e8b<\/a><br \/>\n\u3067google\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u306e\u3067\u3001<br \/>\n\u305d\u306egoogle\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3059\u308b\u3002\n<\/p>\n<p>[python]<br \/>\ndef getallalbums(google, nexttoken=None):<br \/>\n    data = {<br \/>\n        &#8220;pageToken&#8221;:nexttoken<br \/>\n    }<br \/>\n    response = google.get(albums_url, params=data)<br \/>\n    response_json = response.json()<br \/>\n    for l in response_json[&#8220;albums&#8221;]:<br \/>\n        print(l[&#8220;title&#8221;])<br \/>\n    nexttoken = response_json.get(&#8220;nextPageToken&#8221;)<br \/>\n    if nexttoken:<br \/>\n        getallalbums(google, nexttoken)<\/p>\n<p>[\/python]<\/p>\n<p>\n\u3053\u308c\u3067\u6a19\u6e96\u51fa\u529b\u306b\u30a2\u30eb\u30d0\u30e0\u304c\u4e00\u89a7\u8868\u793a\u3055\u308c\u308b\u3002\n<\/p>\n<h2>\ngoogle photo api\u3067\u30a2\u30eb\u30d0\u30e0\u306e\u4f5c\u6210<br \/>\n<\/h2>\n<p>\n\u30a2\u30eb\u30d0\u30e0\u4f5c\u6210\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3001google\u306f\u8a8d\u8a3c\u5f8c\u306egoogle instance\u3092\u6307\u5b9a\u3057\u3066\u3001name\u306f\u30a2\u30eb\u30d0\u30e0\u540d\u3092\u6307\u5b9a\u3002\n<\/p>\n<p>[python]<br \/>\ndef create_albums(google, name):<br \/>\n    data = {<br \/>\n        &#8220;album&#8221;:{<br \/>\n            &#8220;title&#8221;: name<br \/>\n        }<br \/>\n    }<br \/>\n    data = json.dumps(data)<br \/>\n    r = google.post(albums_url, data=data)<br \/>\n    print(r.content)<br \/>\n    r = r.json()<br \/>\n    albumid = r[&#8220;id&#8221;]<br \/>\n    print(albumid)<\/p>\n<p>[\/python]<\/p>\n<h2>\nGoogle photo api\u3067\u5199\u771f\u3092\u30a2\u30eb\u30d0\u30e0\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<br \/>\n<\/h2>\n<p>\n\u5199\u771f\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3001\uff12\u56deAPI\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\u5199\u771f\u540d\u306f\u65e5\u672c\u8a9e\u3067\u3082\u3044\u3044\u3088\u3046\u306butf-8\u3067\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u3002\n<\/p>\n<p>[python]<br \/>\ndef upload(google, album_id, image, name):<br \/>\n# image\u306fdict\u3067image[&#8220;name&#8221;]\u3067\u5199\u771f\u306e\u540d\u524d\u3001image[&#8220;path&#8221;]\u5199\u771f\u304c\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u30d5\u30eb\u30d1\u30b9\u3092\u60f3\u5b9a\u3002<br \/>\n        headers = {<br \/>\n            &#8216;Authorization&#8217;: &#8220;Bearer &#8221; + str(google.token),<br \/>\n            &#8216;Content-Type&#8217;: &#8216;application\/octet-stream&#8217;,<br \/>\n            &#8216;X-Goog-Upload-File-Name&#8217;: image[&#8220;name&#8221;].encode(&#8220;utf-8&#8221;),<br \/>\n            &#8216;X-Goog-Upload-Protocol&#8217;: &#8220;raw&#8221;,<br \/>\n        }<\/p>\n<p>        with open(str(image[&#8220;path&#8221;]), &#8216;rb&#8217;) as image_data:<br \/>\n# \u5199\u771f\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<br \/>\n            response = google.post(upload_url, headers=headers, data=image_data)<br \/>\n            r = response.content<br \/>\n            data = {<br \/>\n                &#8216;albumId&#8217;: album_id,<br \/>\n                &#8220;newMediaItems&#8221;:[{<br \/>\n                            &#8220;simpleMediaItem&#8221;:<br \/>\n                                {&#8220;uploadToken&#8221;:r.decode(&#8220;utf-8&#8221;)}<br \/>\n                        }<br \/>\n                    ]<br \/>\n                }<br \/>\n            data = json.dumps(data)<br \/>\n# \u5199\u771f\u306e\u767b\u9332<br \/>\n            google.post(media_url, data=data)<br \/>\n[\/python]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u56de\u306e\u8a18\u4e8b \u3067google photo api\u306e\u8a8d\u8a3c\u65b9\u6cd5\u3092\u307e\u3068\u3081\u305f\u3002 \u3053\u3053\u3067\u306f\u8a8d\u8a3c\u5f8c\u306b\u30a2\u30eb\u30d0\u30e0\u4f5c\u6210\u3001\u30a2\u30eb\u30d0\u30e0\u53d6\u5f97\u3001\u30a2\u30eb\u30d0\u30e0\u3078\u306e\u5199\u771f\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u65b9\u6cd5\u306e\u305f\u3081\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u305f\u306e\u3067\u4ee5\u4e0b\u306b\u307e\u3068\u3081\u308b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,7],"tags":[],"class_list":["post-1266","post","type-post","status-publish","format-standard","hentry","category-20","category-7"],"_links":{"self":[{"href":"http:\/\/ts-engine.net\/index.php?rest_route=\/wp\/v2\/posts\/1266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ts-engine.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ts-engine.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ts-engine.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ts-engine.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1266"}],"version-history":[{"count":0,"href":"http:\/\/ts-engine.net\/index.php?rest_route=\/wp\/v2\/posts\/1266\/revisions"}],"wp:attachment":[{"href":"http:\/\/ts-engine.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ts-engine.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1266"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ts-engine.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}