iTranslated by AI
Solving the missing_scope Error in Slack Bolt SDK's files.uploadV2()
Introduction
I was developing a Slack app using Slack's Bolt and encountered a missing_scope error when using files.uploadV2() to upload files.
Environment
- node 18.14.2
- typescript 5.0.2
- @slack/bolt 3.12.2
The Error
Originally, I was using .upload() (V1).
await client.files.upload({
channels: event.channel,
file: buffer,
thread_ts: event.thread_ts ?? event.ts,
filename: fileName,
});
While there were no particular issues, an INFO message appeared suggesting that I should use V2.
bolt-app Our latest recommendation is to use client.files.uploadV2() method, which is mostly compatible and much stabler, instead.
So I replaced it with V2.
await client.files.uploadV2({ // Switched to V2
channels: event.channel,
file: buffer,
thread_ts: event.thread_ts ?? event.ts,
filename: fileName,
});
Then the following error occurred.
An API error occurred: missing_scope
The files.info API method is the culprit that requires the files.read scope.
It seems that when using V2, files.read permission is required. In fact, I had granted files.write to the Slack App, but not files.read. However, I wanted to keep permissions to a minimum and avoid granting files.read if possible.
Also, I received the following WARN message in another matter. I want to fix this as well.
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').
Solution
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, // Turn off because the `files.read` permission is not attached
});
There was "good news" in the issue mentioned earlier.
0
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:
As written, setting request_file_info to false allowed me to avoid the error. This way, I no longer need to attach the files.read permission.
Also, I changed channels to channel_id to address the WARN that channels is deprecated in V2.
Conclusion
I hope this is helpful.
Discussion