Skip to content

Instantly share code, notes, and snippets.

@okapies
Last active October 3, 2023 10:18
Show Gist options
  • Star 80 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save okapies/eab5c6fc217e914ed0cac6c944384e4d to your computer and use it in GitHub Desktop.
Save okapies/eab5c6fc217e914ed0cac6c944384e4d to your computer and use it in GitHub Desktop.
Mastodon API の叩き方

Mastodon の API を叩くには以下の手順を踏む必要がある:

  1. OAuth2 クライアントを登録する
  2. アクセストークンを取得する
  3. アクセストークンを Authorization ヘッダに指定して API にアクセスする

OAuth2 クライアント登録

Mastodon の Apps API に登録情報を送ってクライアントを払い出してもらう(一度だけやれば OK).

MASTODON_HOST=...
CLIENT_NAME=...
curl -X POST -sS https://${MASTODON_HOST}/api/v1/apps \
  -F "client_name=${CLIENT_NAME}" \
  -F "redirect_uris=urn:ietf:wg:oauth:2.0:oob" \
  -F "scopes=read write follow"

レスポンスとして以下のような JSON が返ってくるので記録しておく:

{
  "id":????,
  "redirect_uri":"urn:ietf:wg:oauth:2.0:oob",
  "client_id":"...",
  "client_secret":"..."
}

アクセストークン取得

払い出された client_idclient_secret を使って Mastodon インスタンスから認可を受け、アクセストークンを取得する. usernamepassword には、とりあえず自分のアカウントを入れる. scope パラメータ(scopes ではない. パラメータ名が違うので注意)は、クライアント登録時に指定した scope のみ指定できる(空白区切りなので、複数指定する時は %20 で区切る).

(注: アプリケーションに組み込む時は、password ではなく他の grant_type を使うこと。生パスとかありえないので)

MASTODON_HOST=...
CLIENT_ID=...
CLIENT_SECRET=...
USERNAME=...
PASSWORD=...
SCOPE=read%20write%20follow
curl -X POST -sS https://${MASTODON_HOST}/oauth/token \
  -d "client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&grant_type=password&username=${USERNAME}&password=${PASSWORD}&scope=${SCOPE}"

認可が成功するとアクセストークンが払い出される(有効期限はインスタンス次第?):

{
  "access_token":"...",
  "token_type":"bearer",
  "scope":"read",
  "created_at":1492245263
}

また、初回の認可が成功すると、「認証済みアプリ」のページ (https://${MASTODON_HOST}/oauth/authorized_applications) にクライアントが登録される.

API アクセス

API にアクセスする際は、Authorization ヘッダを追加して払い出されたアクセストークンを指定する:

Authorization: Bearer [ACCESS_TOKEN]

例えば、自分のタイムラインにアクセスするには以下のようなリクエストを投げる:

MASTODON_HOST=...
ACCESS_TOKEN=...
curl -sS https://${MASTODON_HOST}/api/v1/timelines/home --header "Authorization: Bearer ${ACCESS_TOKEN}"

トゥート(投稿)する場合は以下のようにする(空白や日本語はパーセントエンコーディングする必要があることに注意):

MASTODON_HOST=...
ACCESS_TOKEN=...
STATUS=Mastodon+%E3%81%9F%E3%83%BC%E3%81%AE%E3%81%97%E3%83%BC%EF%BC%81
curl -X POST -Ss https://${MASTODON_HOST}/api/v1/statuses \
  --header "Authorization: Bearer ${ACCESS_TOKEN}" \
  -d "status=${STATUS}"

レスポンスはこんな感じ:

{
  "id": 511943,
  "created_at": "2017-04-16T06:37:45.215Z",
  "in_reply_to_id": null,
  "in_reply_to_account_id": null,
  "sensitive": null,
  "spoiler_text": "",
  "visibility": "public",
  "application": {
    "name": "akka-ostatus",
    "website": null
  },
  "account": {
    "id": 5166,
    "username": "okapies",
    "acct": "okapies",
    "display_name": "",
    "locked": false,
    "created_at": "2017-04-14T14:28:53.202Z",
    "followers_count": 12,
    "following_count": 13,
    "statuses_count": 27,
    "note": "",
    "url": "https://pawoo.net/@okapies",
    "avatar": "https://img.pawoo.net/accounts/avatars/000/005/166/original/496aedb9c97090b8.png?1492232471",
    "avatar_static": "https://img.pawoo.net/accounts/avatars/000/005/166/original/496aedb9c97090b8.png?1492232471",
    "header": "/headers/original/missing.png",
    "header_static": "/headers/original/missing.png"
  },
  "media_attachments": [],
  "mentions": [],
  "tags": [],
  "uri": "tag:pawoo.net,2017-04-16:objectId=511943:objectType=Status",
  "content": "<p>Mastodon たーのしー!</p>",
  "url": "https://pawoo.net/@okapies/511943",
  "reblogs_count": 0,
  "favourites_count": 0,
  "reblog": null,
  "favourited": false,
  "reblogged": false
}

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment