> [!attention]
> 細かいところ、よしなにできそうなところは省略。ポイントだけ。
Slackアプリの設定画面を開いている前提で。
## Redirect URLの設定
`Features > OAuth & Permissions`で`Redirect URLs`を設定する。Webサービスなら認証制御用のpathに飛ばせばいい。今回は諸事情により`localhost`。
![[Pasted image 20220509000623.png]]
## Sharable URLにアクセス
`Settings > Manage Distribution`の`Sharable URL`にアクセスする。`Sharable URL`は以下のような感じ。
`https://slack.com/oauth/v2/authorize?client_id=<client_id>&scope=&user_scope=<user_scopes>`
- `<client_id>`は`Settings > Basic Information`を参照
- `<user_scopes>`は`Features > OAuth & Permissions`の`Scopes`設定が反映される
こんな画面が出るので権限を確認して `Allow`。
![[Pasted image 20220509000455.png]]
## codeの取得
`Allow`を押した後にリダイレクトされるので、[[URL]]から`code`を引き抜く。
```
https://localhost/?code=<code>&state=
```
## アクセストークンの取得
`code`を使って`POST https://slack.com/api/oauth.v2.access`にリクエストする。
```bash
curl --location --request POST 'https://slack.com/api/oauth.v2.access' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=<client_id>' \
--data-urlencode 'client_secret=<client_secret>' \
--data-urlencode 'code=<code>' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'redirect_uri=<redirect_uri>'
```
- `<client_id>`と`<client_secret>`は`Settings > Basic Information`を参照``
- `<code>`は[[#codeの取得]]で取得したものを使う
- `<redirect_uri>`は[[#Redirect URLの設定]]で設定したものを使う
```json
{
"ok": true,
"app_id": <app_id>,
"authed_user": {
"id": <user_id>,
"scope": <scopes>,
"access_token": <access_token>,
"token_type": "user"
},
"team": {
"id": <team_id>,
"name": <team_name>
},
"enterprise": null,
"is_enterprise_install": false
}
```
`authed_user.access_token`の値をアクセストークンとして取得する。
> [!error]- oauth_authorization_url_mismatch
> 以下のようなエラーになる場合。
> ```json
> {
> "ok": false,
> "error": "oauth_authorization_url_mismatch"
> }
> ```
> [[#Redirect URLの設定]]で使った[[OAuth]]と、今回リクエストの[[OAuth]]バージョンが違う。
## アクセストークンをローテーションさせる
一連の処理で取得できるアクセストークン、実は毎回同じ値なのであまり意味がない。定期的ローテーションさせることで、毎回取得する意味を持たせる。
#todo
## 参考
- [Darkを使用して、Slack App作成する \- Qiita](https://qiita.com/Morero/items/2c954b6e7acb23b0dafe)