LoginSignup
7
7

More than 5 years have passed since last update.

Mastodon APIをcurlで試してみる

Last updated at Posted at 2017-04-17

ハマったところはアクセストークンの取得で、何故かログインできない感じでアクセストークンの取得がうまく行かなかったのですが、原因は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

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7