Node-RedからGoogle Driveに画像ファイルをアップロードする


### 1. Google Developer Consoleで APIの有効化・サービスアカウント作成し、JSONファイルをダウンロード

[※参考記事](https://docs.biztex.co.jp/cobit-docs/google_spreadsheet_settings/for_serviceaccount.html)


**1-1. [Google Developer Console](https://console.developers.google.com/apis/dashboard)にアクセスする**

**1-2. 上部メニューのプロジェクトを選択を選ぶ**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/29c04c06-602e-25fe-6baf-44d927c17b70.png)

**1-3. 新しいプロジェクトをクリック**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/a7c23775-5225-45f4-b98f-05658814e432.png)

**1-4. プロジェクト名を入力して作成ボタンを押す**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/cf4897d7-2a48-59b1-2325-70f70002b4bb.png)

**1-5. 作成したプロジェクトを選択し、Google Drive APIを有効にする**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/42f72bc9-c0a8-cea3-268f-0d1c1c75ae7e.png)


**1-6. サービスアカウントを作成**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/d01627cc-e547-268a-3240-cb0d5d329f27.png)


**1-7. サービスアカウントの編集画面から鍵を作成し**JSONファイル**をダウンロードする**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/1abd0462-02fe-6700-0655-f8e6ff775647.png)


### 2. Google Driveでフォルダを作成し、フォルダを先ほど作成したサービスアカウントに共有する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/2bea69e0-d5bc-cebc-a46c-08f8a0822505.png)



### 3.作成したフォルダのURLからフォルダIDを取得

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/d3408817-c66f-d9aa-342f-0cb253ea566f.png)


### 4. Node-Redのパレットから「node-red-contrib-google」ノードをインストール
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/bc8e3bdb-e38c-298d-cd49-6a0c152034bd.png)


### 5. フローを読み込み、各ノードブロックのプロパティを設定する

```
[{"id":"37fe8973.9b3846","type":"change","z":"d7300f51.c03af","name":"set parameters","rules":[{"t":"set","p":"folderid","pt":"msg","to":"","tot":"str"},{"t":"set","p":"filename","pt":"msg","to":"test.jpg","tot":"str"},{"t":"set","p":"mimeType","pt":"msg","to":"image/jpg","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{\t    \"resource\": {\t        \"name\": filename,\t        \"parents\": [\t            folderid\t        ]\t    },\t    \"media\": {\t        \"mimeType\": mimeType,\t        \"body\": payload\t    },\t    \"fields\": \"id\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":140,"wires":[["d862bfac.a32a7"]]},{"id":"39a1f760.f13da8","type":"debug","z":"d7300f51.c03af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":140,"wires":[]},{"id":"d862bfac.a32a7","type":"google","z":"d7300f51.c03af","name":"","google":"be4a5517.9547d8","api":"drive:v3","operation":"files.create","x":470,"y":140,"wires":[["39a1f760.f13da8"]]},{"id":"caf7c056.9ef8d","type":"file in","z":"d7300f51.c03af","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":470,"y":80,"wires":[["37fe8973.9b3846"]]},{"id":"7d1550df.83376","type":"change","z":"d7300f51.c03af","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"/home/pi/Pictures/test/test.jpg","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":80,"wires":[["caf7c056.9ef8d"]]},{"id":"e8c966be.6e9248","type":"inject","z":"d7300f51.c03af","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":80,"wires":[["7d1550df.83376"]]},{"id":"be4a5517.9547d8","type":"google-conn","z":"","name":"Google","key":"{}\n","scopes":"https://www.googleapis.com/auth/drive\nhttps://www.googleapis.com/auth/drive.appdata\nhttps://www.googleapis.com/auth/drive.file\nhttps://www.googleapis.com/auth/drive.metadata\nhttps://www.googleapis.com/auth/drive.metadata.readonly\nhttps://www.googleapis.com/auth/drive.photos.readonly\nhttps://www.googleapis.com/auth/drive.readonly\nhttps://www.googleapis.com/auth/drive.scripts"}]
```

**googleノードブロック**：JSON Keyを**手順1-7**で取得したJSONファイルの内容で設定
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/9288b595-09e0-2e74-95ca-153bb345645e.png)




**「set parameters」ブロック**：「folderid」に**手順3**で取得したフォルダIDを設定
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/4b1561ae-57a9-a4bb-e604-ac1f9413afb5.png)

**「set msg.filename」ブロック**：送信対象の画像ファイルのパスを設定
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/411dbfeb-6ede-d35c-79fb-871b4143e6b4.png)

### 6. injectノードを実行してGoogle Driveにファイルがアップロードされていることを確認

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599283/97bf61af-62d8-a9a7-fc95-6e90694600c9.png)