{"id":4694,"date":"2024-03-14T17:10:36","date_gmt":"2024-03-14T08:10:36","guid":{"rendered":"https:\/\/taitan916.info\/blog\/?p=4694"},"modified":"2024-03-29T17:48:08","modified_gmt":"2024-03-29T08:48:08","slug":"post-4694","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/4694","title":{"rendered":"PHP\u304b\u3089DBX Platform\u3092\u5229\u7528\u3057\u3066\u30b5\u30fc\u30d0\u5185\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>PHP\u3067\u4f55\u3089\u304b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3060\u3068\u4eca\u307e\u3067\u306f\u30ed\u30fc\u30ab\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u3001\u3082\u3057\u304f\u306f\u30e1\u30fc\u30eb\u306b\u6dfb\u4ed8\u3059\u308b\u5f62\u3092\u53d6\u3063\u3066\u3044\u305f\u3002\u4eca\u56de\u65b0\u305f\u306a\u9078\u629e\u80a2\u3068\u3057\u3066Dropbox\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u305f\u3044\u3002\u8abf\u3079\u305f\u3068\u3053\u308dDBX Platform\u3068\u3044\u3046\u30b5\u30fc\u30d3\u30b9\u304c\u3042\u308a\u3001\u3053\u3061\u3089\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u5b9f\u88c5\u3067\u304d\u308b\u6a21\u69d8\u3002\u4ee5\u4e0b\u306b\u5b9f\u88c5\u65b9\u6cd5\u3092\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Dropbox\u5074\u306e\u6e96\u5099<\/h2>\n<h3>\u30a2\u30d7\u30ea\u306e\u4f5c\u6210<\/h3>\n<p>\u4ee5\u4e0b\u30da\u30fc\u30b8\u306b\u3066\u300c\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.dropbox.com\/developers\" target=\"_blank\" rel=\"noopener\">https:\/\/www.dropbox.com\/developers<\/a><\/p>\n<p>\u4ee5\u4e0b\u5185\u5bb9\u3067\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3059\u308b(Name your app\u306f\u9069\u5b9c\u5909\u66f4)\u3002<\/p>\n<p><a href=\"https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/001-8.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-4695\" src=\"https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/001-8-300x163.jpg\" alt=\"\" width=\"300\" height=\"163\" srcset=\"https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/001-8-300x163.jpg 300w, https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/001-8-1024x556.jpg 1024w, https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/001-8-768x417.jpg 768w, https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/001-8.jpg 1130w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a<\/h3>\n<p>\u300cPermissions\u300d\u30bf\u30d6\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u4ee5\u4e0b\u306e\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3\u8a2d\u5b9a\u3092\u884c\u3046\u3002<\/p>\n<p><a href=\"https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/002-6.jpg\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-4696\" src=\"https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/002-6-297x300.jpg\" alt=\"\" width=\"297\" height=\"300\" srcset=\"https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/002-6-297x300.jpg 297w, https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/002-6-100x100.jpg 100w, https:\/\/taitan916.info\/blog\/wp-content\/uploads\/2024\/02\/002-6.jpg 768w\" sizes=\"auto, (max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<h3>App key\u53ca\u3073App secret\u306e\u53d6\u5f97<\/h3>\n<p>\u300cSettings\u300d\u30bf\u30d6\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u30bb\u30c3\u30c6\u30a3\u30f3\u30b0\u30da\u30fc\u30b8\u5185\u306e\u300cApp key\u300d\u300cApp secret\u300d\u3092\u30e1\u30e2\u3057\u3066\u304a\u304f\u3002<\/p>\n<h3>\u30a2\u30af\u30bb\u30b9\u30b3\u30fc\u30c9\u306e\u53d6\u5f97<\/h3>\n<p>\u4ee5\u4e0b\u3092\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u958b\u304d\u30a2\u30af\u30bb\u30b9\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3002\u3010\u3011\u5185\u3092\u9069\u5b9c\u5909\u66f4\u3059\u308b\u3053\u3068\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">https:\/\/www.dropbox.com\/oauth2\/authorize?token_access_type=offline&amp;response_type=code&amp;client_id=\u3010App key\u3011<\/pre>\n<h3>\u30ea\u30d5\u30ec\u30c3\u30b7\u30e5\u30c8\u30fc\u30af\u30f3\u306e\u53d6\u5f97<\/h3>\n<p>\u30b5\u30fc\u30d0\u306bSSH\u63a5\u7d9a\u3057\u4ee5\u4e0b\u30b3\u30de\u30f3\u30c9\u3067\u30ea\u30d5\u30ec\u30c3\u30b7\u30e5\u30c8\u30fc\u30af\u30f3\u3092\u53d6\u5f97\u3059\u308b\u3002\u3010\u3011\u5185\u3092\u9069\u5b9c\u5909\u66f4\u3059\u308b\u3053\u3068\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">curl https:\/\/api.dropbox.com\/oauth2\/token -d code=\u3010\u30a2\u30af\u30bb\u30b9\u30b3\u30fc\u30c9\u3011 -d grant_type=authorization_code -u \u3010App key\u3011:\u3010App secret\u3011<\/pre>\n<p>\u6210\u529f\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306aJSON\u30c7\u30fc\u30bf\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067refresh_token\u90e8\u5206\u3092\u30e1\u30e2\u3063\u3066\u304a\u304f\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">{\r\n    \"access_token\": \"xxxxxxxxxxxxxxxxxxxxx\",\r\n    \"token_type\": \"bearer\",\r\n    \"expires_in\": 14400,\r\n    \"refresh_token\": \"xxxxxxxxxxxxxxxxxxxxx\",\r\n    \"scope\": \"xxxxxxxxxxxxxxxxxxxxx\",\r\n    \"uid\": \"xxxxxxxxxxxxxxxxxxxxx\",\r\n    \"account_id\": \"xxxxxxxxxxxxxxxxxxxxx\"\r\n}<\/pre>\n<p>\u307e\u305f\u3001\u3053\u306e\u6642\u70b9\u3067\u4f5c\u6210\u3057\u305f\u30a2\u30d7\u30ea\u3068\u7d10\u3065\u304f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304c\u751f\u6210\u3055\u308c\u308b\u7b48\u3002\u4eca\u56de\u306e\u5834\u5408\u306f\u300c\/\u30a2\u30d7\u30ea\/php_connection_test\u300d\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304c\u751f\u6210\u3055\u308c\u3001\u3053\u3061\u3089\u304c\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u306a\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>PHP\u5074<\/h2>\n<h3>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\nconst APP_KEY = '\u3010App key\u3011';\r\nconst APP_SECRET = '\u3010App secret\u3011';\r\nconst REFRESH_TOKEN = '\u3010\u30ea\u30d5\u30ec\u30c3\u30b7\u30e5\u30c8\u30fc\u30af\u30f3\u3011';\r\n\r\n\/\/\u4e00\u6642\u7684\u306a\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u53d6\u5f97\r\nfunction getAccessToken() {\r\n    $url = 'https:\/\/api.dropboxapi.com\/oauth2\/token';\r\n    $data = [\r\n        'grant_type'    =&gt; 'refresh_token',\r\n        'refresh_token' =&gt; REFRESH_TOKEN,\r\n        'client_id'     =&gt; APP_KEY,\r\n        'client_secret' =&gt; APP_SECRET\r\n    ];\r\n\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL, $url);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));\r\n\r\n    $res = curl_exec($ch);\r\n    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\r\n\r\n    $access_token = '';\r\n    if( !curl_errno($ch) &amp;&amp; $http_code == \"200\" ){\r\n        $res = json_decode($res, true);\r\n        $access_token = $res['access_token'];\r\n    } else {\r\n        echo \"ERROR: Failed to access Dropbox API : \" . curl_error($ch) . \"&lt;br&gt;\";\r\n    }\r\n\r\n    curl_close($ch);\r\n\r\n    return $access_token;\r\n}\r\n\r\n\/\/\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\r\nfunction uploadFile($access_token, $local_filepath, $upload_filepath) {\r\n    $url = \"https:\/\/content.dropboxapi.com\/2\/files\/upload\";\r\n    $headers = array(\r\n        \"Authorization: Bearer \" . $access_token, \/\/ \u53d6\u5f97\u3057\u305f\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\r\n        \"Content-Type: application\/octet-stream\",\r\n        \"Dropbox-API-Arg: \" . json_encode(array(\r\n            \"path\" =&gt; $upload_filepath,\r\n            \"mode\" =&gt; \"add\",\r\n            \"autorename\" =&gt; true\r\n        )),\r\n    );\r\n    $file = fopen($local_filepath, 'rb');\r\n    $size = filesize($local_filepath);\r\n\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL, $url);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    curl_setopt($ch, CURLOPT_PUT, true);\r\n    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');\r\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n    curl_setopt($ch, CURLOPT_INFILE, $file);\r\n    curl_setopt($ch, CURLOPT_INFILESIZE, $size);\r\n    $res = curl_exec($ch);\r\n    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\r\n\r\n    if (!curl_errno($ch) &amp;&amp; $http_code == \"200\") {\r\n        $flg = true;\r\n    } else {\r\n        $flg = false;\r\n    }\r\n\r\n    fclose($file);\r\n    curl_close($ch);\r\n\r\n    return $flg;\r\n}\r\n\r\n\/\/\u4e00\u6642\u7684\u306a\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u53d6\u5f97\r\n$access_token = getAccessToken();\r\n\r\n\/\/\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u30d5\u30a1\u30a4\u30eb(\u76f8\u5bfe\u30d1\u30b9\u3001\u7d76\u5bfe\u30d1\u30b9\u3069\u3061\u3089\u3067\u3082OK)\r\n$local_file = '.\/002.jpg';\r\n\r\n\/\/\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5148\u306e\u30d5\u30a1\u30a4\u30eb\u540d(\u30a2\u30d7\u30ea\u30d5\u30a9\u30eb\u30c0\u304c\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u306a\u308b)\r\n$upload_file = '\/002.jpg';\r\n\r\n$flg = uploadFile($access_token, $local_file, $upload_file);\r\n\r\nif( $flg ){\r\n    echo 'success';\r\n}else{\r\n    echo 'error';\r\n}<\/pre>\n<h3>\u6ce8\u610f\u70b9<\/h3>\n<p>\u30ed\u30fc\u30ab\u30eb\u5074\u306e\u30d5\u30a1\u30a4\u30eb($local_file)\u306f\u76f8\u5bfe\u30d1\u30b9\u3001\u7d76\u5bfe\u30d1\u30b9\u3069\u3061\u3089\u3067\u3082\u554f\u984c\u7121\u3044\u304c\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5148\u306e\u30d5\u30a1\u30a4\u30eb\u540d($upload_file)\u306f\u300c\/\u30a2\u30d7\u30ea\/php_connection_test\u300d\u3068\u3044\u3046\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304b\u3089\u898b\u305f\u30d1\u30b9\u306b\u306a\u308b\u305f\u3081\u300c\/xxxxxx.jpg\u300d\u306e\u3088\u3046\u306a\u5f62\u3067\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30a8\u30e9\u30fc\u5bfe\u5fdc<\/h2>\n<h3>Your app is not permitted to access this endpoint because it does not have the required scope 'files.content.write'. The owner of the app can enable~~ \u30a8\u30e9\u30fc<\/h3>\n<p>\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3\u8a2d\u5b9a\u3067\u300cfiles.content.write\u300d\u306b\u30c1\u30a7\u30c3\u30af\u3092\u5165\u308c\u5fd8\u308c\u3066\u3044\u305f\u305f\u3081\u767a\u751f\u3057\u305f\u6a21\u69d8\u3002<\/p>\n<p>\u5c1a\u3001\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3\u8a2d\u5b9a\u5f8c\u306f\u518d\u5ea6\u300c\u30a2\u30af\u30bb\u30b9\u30b3\u30fc\u30c9\u306e\u53d6\u5f97\u2192\u30ea\u30d5\u30ec\u30c3\u30b7\u30e5\u30c8\u30fc\u30af\u30f3\u306e\u53d6\u5f97\u300d\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308b\u70b9\u306b\u6ce8\u610f\u3059\u308b\u3002<\/p>\n<h3>missing_scope\u30a8\u30e9\u30fc<\/h3>\n<p><a href=\"https:\/\/www.dropboxforum.com\/t5\/Dropbox-API-Support-Feedback\/Upload-a-file-via-Dropbox-API-and-CURL\/td-p\/457629\" target=\"_blank\" rel=\"noopener\">\u3053\u3061\u3089\u306e\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u30da\u30fc\u30b8<\/a>\u306b\u3088\u308b\u3068\u300cApp Console \u3092\u4ecb\u3057\u3066\u30a2\u30d7\u30ea\u306b\u30b9\u30b3\u30fc\u30d7\u3092\u8ffd\u52a0\u3057\u305f\u3060\u3051\u3067\u306f\u3001\u305d\u306e\u30b9\u30b3\u30fc\u30d7\u304c\u65e2\u5b58\u306e\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u306b\u9061\u53ca\u7684\u306b\u4ed8\u4e0e\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u300d\u3068\u3042\u308b\u3002<\/p>\n<p>\u3053\u308c\u306f\u4e0a\u8a18\u306e\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3\u8a2d\u5b9a\u5909\u66f4\u5f8c\u3001\u300c\u30a2\u30af\u30bb\u30b9\u30b3\u30fc\u30c9\u306e\u53d6\u5f97\u2192\u30ea\u30d5\u30ec\u30c3\u30b7\u30e5\u30c8\u30fc\u30af\u30f3\u306e\u53d6\u5f97\u300d\u3092\u884c\u308f\u306a\u304b\u3063\u305f\u305f\u3081\u767a\u751f\u3057\u305f\u6a21\u69d8\u3002<\/p>\n<p>\u300c\u30a2\u30af\u30bb\u30b9\u30b3\u30fc\u30c9\u306e\u53d6\u5f97\u2192\u30ea\u30d5\u30ec\u30c3\u30b7\u30e5\u30c8\u30fc\u30af\u30f3\u306e\u53d6\u5f97\u300d\u3092\u518d\u5ea6\u884c\u3046\u3053\u3068\u3067\u89e3\u6c7a\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u53c2\u8003\u30b5\u30a4\u30c8<\/h2>\n<p><a href=\"https:\/\/comment-out.net\/2023\/08\/25\/1924.html\" target=\"_blank\" rel=\"noopener\">https:\/\/comment-out.net\/2023\/08\/25\/1924.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u3067\u4f55\u3089\u304b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3060\u3068\u4eca\u307e\u3067\u306f\u30ed\u30fc\u30ab\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u3001\u3082 ... <\/p>\n","protected":false},"author":1,"featured_media":3853,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[103],"class_list":["post-4694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-dropbox"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/4694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=4694"}],"version-history":[{"count":4,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/4694\/revisions"}],"predecessor-version":[{"id":4700,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/4694\/revisions\/4700"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/3853"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=4694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=4694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=4694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}