Slack Bolt(SDK)のfiles.uploadV2()でmissing_scopeエラーが発生する

2023/12/08に公開

はじめに

SlackのBoltを使ってSlackアプリを開発しており、ファイルをアップロードする際に files.uploadV2() を使うと missing_scope エラーが発生しました。

環境

  • node 18.14.2
  • typescript 5.0.2
  • @slack/bolt 3.12.2

発生したエラー

もともと .upload() (つまりV1)を使っていました。

await client.files.upload({
  channels: event.channel,
  file: buffer,
  thread_ts: event.thread_ts ?? event.ts,
  filename: fileName,
});

特に問題なかったのですが、V2使った方がいいよ〜というINFOが出ます。

bolt-app Our latest recommendation is to use client.files.uploadV2() method, which is mostly compatible and much stabler, instead.

ということでV2に置き換えました。

await client.files.uploadV2({ // V2にした
  channels: event.channel,
  file: buffer,
  thread_ts: event.thread_ts ?? event.ts,
  filename: fileName,
});

すると以下のエラーが発生。

An API error occurred: missing_scope

`files.uploadV2` returns `missing_scope` error but still posts message · Issue #1620 · slackapi/node-slack-sdk

The files.info API method is the culprit that requires the files.read scope.

V2を使った場合、 files.read の権限が必要になるようです。実際、Slack Appに files.write は与えていましたが files.read は与えていませんでした。ただ、権限はできるだけ少なくしたいので、 files.read を与えるのは避けたいなーと思っていました。

また、別件で以下のWARNが出ました。こちらも直したい。

bolt-app Although the 'channels' parameter is still supported for smoother migration from legacy files.upload, we recommend using the new channel_id parameter with a single str value instead (e.g. 'C12345').

解決策

await client.files.uploadV2({
- channels: event.channel,
+ channel_id: event.channel,
  file: buffer,
  thread_ts: event.thread_ts ?? event.ts,
  filename: fileName,
+ request_file_info: false, // `files.read` 権限をアタッチしていないためオフにする
});

先程のIssueに「ただし良い知らせがある」と。

`files.uploadV2` returns `missing_scope` error but still posts message · Issue #1620 · slackapi/node-slack-sdk

The good news is that this is an optional step and you can disable it by passing in the option request_file_info: false to the files.uploadV2 function:

書かれているように request_file_infofalse にするとエラーを回避できました。これで files.read 権限をアタッチしなくてもよくなりました。

また、 channels はV2では非推奨というWARNの対応として channel_id に変更しました。

おわりに

以上、参考になれば幸いです。

Discussion