おはやし日記

テーマ……バイク←プログラミング←旅

mstdn.jpのアプリケーションのユーザー認証

メモ。

アプリの登録

  • client_name -> アプリ名
  • scopes -> アプリの権限。複数あるとき空白区切りでいいらしい
curl -X POST \
 https://mstdn.jp/api/v1/apps \
 -F 'client_name=ohaToot' \
 -F 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' \
 -F 'scopes=write'

レスポンス

{"id":"602156","name":"ohaToot","website":null,"redirect_uri":"urn:ietf:wg:oauth:2.0:oob","client_id":"CLIENT_ID","client_secret":"CLIENT_SECRET","vapid_key":"VAPID_KEY"}

CLIENT_IDCLIENT_SECRET等は実際それぞれの値が返ってくる

アプリケーションの認証

https://mstdn.jp/oauth/authorize
?client_id=CLIENT_ID
&scope=write
&redirect_uri=urn:ietf:wg:oauth:2.0:oob
&response_type=code

にブラウザでアクセスする。

  • scope -> OAuth Scopes - Mastodon documentationにあるアプリの権限
  • redirect_url -> 自分でサーバー持ってればそのURLにしてどうこうできるらしいが、できないのでこの変なやつにしておく

f:id:o-treetree:20210319220107p:plain
認証画面

「承認」を押すと、認証コード(authorization code)が出てくる

f:id:o-treetree:20210319220356p:plain
認証コード

redirect_urlに自前のサイトがないのでここが手動コピペになる

アドレスバーを見るとhttps://mstdn.jp/oauth/authorize/native?code=AUTHORIZATION_CODEになっているのでそこでどうにかできるかもしれない。知らんけど。

アクセストークンの取得

  • scope -> アプリケーションに付与した権限内で。
curl -X POST \
https://mstdn.jp/oauth/token \
-F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' \
-F 'grant_type=authorization_code' \
-F 'code=AUTHORIZATION_CODE' \
-F 'scope=write' 

レスポンス

{"access_token":"ACCESS_TOKEN","token_type":"Bearer","scope":"write","created_at":1616159761}

API利用

ようやく色々できるようになった

トゥート投稿statuses - Mastodon documentation

curl -X POST \
https://mstdn.jp/api/v1/statuses \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-d 'status=ほげ' \
-d 'visibility=unlisted'

レスポンスはなんか長いJSONが返ってくる。

{"id":"105916667149699590","created_at":"2021-03-19T13:21:22.245Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"unlisted","language":"hi","uri":"https://mstdn.jp/users/ohys/statuses/105916667149699590","url":"https://mstdn.jp/@ohys/105916667149699590","replies_count":0,"reblogs_count":0,"favourites_count":0,"favourited":false,"reblogged":false,"muted":false,"bookmarked":false,"pinned":false,"content":"\u003cp\u003eほげ\u003c/p\u003e","reblog":null,"application"............

投稿成功。

プライバシーポリシー ・お問い合わせはこちら