--- lang: ja-jp breaks: true --- # Imgur アカウントに関連付けた画像をAPIでアップロード Node.js 2021-05-07 > What files can I upload? What is the size limit? > https://help.imgur.com/hc/en-us/articles/115000083326-What-files-can-I-upload-What-is-the-size-limit- > Imgur APIで学ぶOAuth2.0認証 > https://qiita.com/IKEH/items/c93854c95a863941a188 > Imgur > https://imgur.com/ > Imgur API > https://apidocs.imgur.com/ ## Imgurにログインし、以下のURLにアクセスします。ここで、アプリケーションの登録を行い、「Client ID」と「Client secret」の取得を行います。 https://api.imgur.com/oauth2/addclient ![](https://i.imgur.com/7xPnpAr.png) :::info ※アプリケーション名は適当でよい。 ※認証タイプは、「OAuth 2 authorization with a callback URL」にする。 ※コールバックURLには、POSTMANのコールバックURL「https://oauth.pstmn.io/v1/browser-callback」を指定します。公式マニュアルには、「https://www.getpostman.com/oauth2/callback」と記載されているが、このURLでは取得出来なかった。 ※POSTMAN:https://www.postman.com/ ::: ## 「Client ID」と「Client secret」が取得できます。 ![](https://i.imgur.com/a0Ft3Mt.png) ## POSTMAN にログインし、APIを使用する為の「Access Token」を取得します。必要な項目を入力し、「Get New Access Token」ボタンを押します。 ![](https://i.imgur.com/OoHJsO6.png) :::info | 項目 | 設定値 | 備考 | | ---------------- | ------------------------------------------- | ----:| | Type | OAuth 2.0 | | | Auth URL | https://api.imgur.com/oauth2/authorize | | | Access Token URL | https://api.imgur.com/oauth2/token | | | Client ID | Imgurのアプリケーション登録で取得できる値。 | | | Client Secret | Imgurのアプリケーション登録で取得できる値。 | | ::: ## 「allow」を押します。 ![](https://i.imgur.com/sWLSSiU.png) ## 認証が完了し、Access Token が取得できます。 ![](https://i.imgur.com/nSyL86N.png) ![](https://i.imgur.com/UYfDwnt.png) ## Access Token の更新 ```javascript= const request = require('request'); //ヘッダーを定義 var headers = { //'Content-Type':'application/json', 'Content-Type':'application/x-www-form-urlencoded', }; //オプションを定義 var options = { url: 'https://api.imgur.com/oauth2/token', method: 'POST', headers: headers, //json: true, form: { "refresh_token" : refresh_token, "client_id" : client_id, "client_secret" : client_secret, "grant_type" : "refresh_token", } }; //リクエスト送信 request(options, function (error, response, body) { //コールバックで色々な処理 console.log(response.statusCode); // 200 console.log(response.statusMessage); // OK let content_type = response.headers['content-type']; console.log("content-type:" + content_type); // application/json console.log("response:" + response); console.log("body:" + body); console.log("error:" + error); if (response.statusCode == 200){ if (content_type == "application/json") { const jsonObject = JSON.parse(body); console.log("body_json:" + JSON.stringify(jsonObject, null, " ")); console.log("access_token:" + jsonObject.access_token ); console.log("refresh_token:" + jsonObject.refresh_token ); } } }); ``` ###### tags: `Imgur` `Node.js` `POSTMAN`