💾 Archived View for space.matthewphillips.info › posts › mastodon-from-the-cli captured on 2023-04-26 at 12:56:18. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-04-19)

-=-=-=-=-=-=-

Mastodon from the CLI

These are the steps needed to login to Mastodon and get an access token for posting statuses from the CLI.

I'm going to set a $MASTODON_SERVER environment variable and use that in all of my curl commands. Obviously replace it with your server.

MASTODON_SERVER=c.im

First step is to register an app with your instance. Create a file called data.json with the following content:

{
	"client_name": "my-app",
	"redirect_uris": "urn:ietf:wg:oauth:2.0:oob",
	"scopes": "read write push",
	"website": "http://example.com"
}

Then send that as a POST request to the API:

curl -d "@data.json" \
        -X POST \
        -H "Content-Type: application/json" \
"https://$MASTODON_SERVER/api/v1/apps"

This will give you back a JSON blob with a client_id and client_secret.

Next you'll request a token. This uses password login, with the username being your email address. Replace the relevant parts.

curl -X POST \
        -F 'client_id=$CLIENT_ID' \
        -F 'client_secret=$CLIENT_SECRET' \
        -F 'grant_type=password' \
        -F 'scope=write read' \
        -F 'username=$EMAIL' \
        -F 'password=$PASSWORD' \
"https://$MASTODON_SERVER/oauth/token"

You should get back JSON that includes an access_token. Keep this (I store it in MASTODON_ACCESS_TOKEN) as it's what you'll use in all future API requests.

From here check out the Mastodon API docs.

Mastodon API

I wrote a bash script for posting statuses which you can find here:

mastodon-status