Skip to content

Commit

Permalink
JsonUrlSaver、Slack API の User OAuth Token を引数として受け付ける様にした。
Browse files Browse the repository at this point in the history
  • Loading branch information
Takym committed Aug 9, 2024
1 parent 13f9439 commit de806ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions JsonUrlSaver/JsonUrlSaver/DefaultDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal sealed class DefaultDownloader : IDownloader
private readonly IServiceProvider _services;
private readonly IUrlFileNameConverter _ufn_conv;
private readonly IUrlFilter? _url_filter;
private readonly string? _token;

public DefaultDownloader(ILogger<DefaultDownloader> logger, IServiceProvider services, IConfiguration config, IUrlFileNameConverter ufnConv)
{
Expand All @@ -39,6 +40,8 @@ public DefaultDownloader(ILogger<DefaultDownloader> logger, IServiceProvider ser
if (!string.IsNullOrEmpty(filters)) {
_url_filter = services.GetUrlFilter(filters);
}

_token = config["token"];
}

public void Download(string cacheDir, IUrlSource source)
Expand All @@ -58,6 +61,10 @@ private async ValueTask DownloadAsync(string cacheDir, IUrlSource source)
{
using (var scope = _services.CreateScope())
using (var hc = scope.ServiceProvider.GetRequiredService<HttpClient>()) {
if (_token is not null) {
hc.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token);
}

foreach (var url in source) {
if (_url_filter?.ShouldDownload(url) ?? true) {
Stream src;
Expand Down
23 changes: 20 additions & 3 deletions JsonUrlSaver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,30 @@ ZIP
> JsonUrlSaver.exe zip=<JSON ファイルを含む ZIP ファイルへのパス> dir=<ZIP ファイルの展開先のパス> zipOverwrite=true
```

### Slack 向けフィルタ
### Slack からデータをダウンロードする
Slack からエクスポートして得られるアーカイブの JSON ファイルからファイルをダウンロードする時は、
`filters=slack` を指定して Slack に保管したファイルのみを抽出する事もできます。
`token=...` で User OAuth Token を指定する必要があります。
また、`filters=slack` を指定し、ダウンロードする URL を絞り込みます。
```cmd
> JsonUrlSaver.exe zip=<Slack のアーカイブへのパス> filters=slack
> JsonUrlSaver.exe zip=<Slack のアーカイブへのパス> token=<User OAuth Token> filters=slack
```

ワークスペースのセキュリティを保つ為には、User OAuth Token は使用後に直ちに破棄する様にしてください。

#### User OAuth Token の発行方法
1. <https://api.slack.com/apps/> にアクセスします。
2. 「Create New App」をクリックします。
3. 「From scratch」を選択します。
4. 必要事項を入力してアプリを作成します。
5. 左のメニューの「OAuth & Permissions」をクリックします。
6. 「Scopes」の「Bot Token Scopes」と「User Token Scopes」の両方に「files:read」を追加します。
7. ページ上部の「OAuth Tokens for Your Workspace」セクションからワークスペースへインストールします。
8. 使い終わったら、「Revoke Tokens」ボタンをクリックします。

#### 参考文献
* [https://api.slack.com/web](Using the Slack Web API | Slack)
* [https://api.slack.com/concepts/token-types](Token types | Slack)

### 表示
ダウンロードしたファイルは下記の手順で表示できます。
`mode=openOnly` を付ける場合、事前にダウンロードが必要になります。
Expand Down

0 comments on commit de806ad

Please sign in to comment.