Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Mastodon APIをcurlで試してみる

More than 3 years have passed since last update.

ハマったところはアクセストークンの取得で、何故かログインできない感じでアクセストークンの取得がうまく行かなかったのですが、原因はcurl -H "Content-Type: application/json"としてJSON形式でポストしなかったことにあったみたいです。

# add application
MASTODON_HOST=mstdn.syui.cf
CLIENT_NAME=mstdn-client
if [ ! -f ./client_id.json ];then
    WEBSITE=https://example.com
    curl -X POST -d "client_name=$CLIENT_NAME&redirect_uris=${REDIRECT_URIS:-urn:ietf:wg:oauth:2.0:oob}&scopes=${SCOPES:-read write follow}&website=$WEBSITE" -sS https://${MASTODON_HOST}/api/v1/apps >> ./client_id.json
fi

# get access token
json=`cat ./client_id.json`
CLIENT_ID=`echo $json|jq -r '.client_id'`
CLIENT_SECRET=`echo $json|jq -r '.client_secret'`
YOUR_EMAIL=mail+mastodon@gmail.com
YOUR_PASSWORD=passwd
curl -H "Content-Type: application/json" -X POST -Ss https://$MASTODON_HOST/oauth/token -d "{\"client_id\": \"$CLIENT_ID\", \"client_secret\": \"$CLIENT_SECRET\", \"grant_type\": \"password\", \"username\": \"$YOUR_EMAIL\", \"password\": \"$YOUR_PASSWORD\", \"scope\": \"read write follow\"}"

# use api
if [ ! -f ./access_token.json ];then
    curl -H "Content-Type: application/json" -X POST -Ss https://$MASTODON_HOST/oauth/token -d "{\"client_id\": \"$CLIENT_ID\", \"client_secret\": \"$CLIENT_SECRET\", \"grant_type\": \"password\", \"username\": \"$YOUR_EMAIL\", \"password\": \"$YOUR_PASSWORD\", \"scope\": \"read write follow\"}" >> access_token.json
fi

ACCESS_TOKEN=`cat ./access_token.json|jq -r '.access_token'`
curl -H "Authorization: Bearer $ACCESS_TOKEN" -sS https://$MASTODON_HOST/api/v1/timelines/home

Testing the API with cURL / https://github.com/tootsuite/documentation/blob/master/Using-the-API/Testing-with-cURL.md

mastodon api docs / https://github.com/tootsuite/documentation/tree/master/Using-the-API

2FA OFF / https://github.com/tootsuite/documentation/issues/72

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away