Skip to content

Instantly share code, notes, and snippets.

@raxityo
Created February 21, 2024 15:17
Show Gist options
  • Save raxityo/5c3b5decd8941a4b9d5c92ed19184952 to your computer and use it in GitHub Desktop.
Save raxityo/5c3b5decd8941a4b9d5c92ed19184952 to your computer and use it in GitHub Desktop.
Example implementation of a Mastodon API client
if [ -z $OAUTH_TOKEN ]
then
if [ -z $CLIENT_ID ]
then
APP_INFO=$(curl -s -X POST \
-F 'client_name=Example App' \
-F 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' \
-F 'scopes=read' \
-F 'website=https://example.com' \
https://mastodon.social/api/v1/apps
)
echo $APP_INFO
CLIENT_ID=$(echo $APP_INFO | jq -r '.client_id')
CLIENT_SECRET=$(echo $APP_INFO | jq -r '.client_secret')
echo $CLIENT_ID
echo $CLIENT_SECRET
fi
OAUTH_TOKEN=$(curl -s -X POST \
-F 'client_id=$CLIENT_ID' \
-F 'client_secret=$CLIENT_SECRET' \
-F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' \
-F 'grant_type=client_credentials' \
https://mastodon.social/oauth/token
)
echo $OAUTH_TOKEN
echo "Set oauth token for future calls"
fi
# Lookup an account by webfinger such as @17Christiana@pixelfed.social
ACCOUNT=$(curl -s \
-H 'Authorization $OAUTH_TOKEN' \
https://mastodon.social/api/v1/accounts/lookup?acct=$WEBFINGER
)
# Print account info
echo $ACCOUNT | jq
ACCOUNT_ID=$(echo $ACCOUNT | jq '.id')
# Get statuses for an account
STATUSES=$(curl -s \
-H 'Authorization $OAUTH_TOKEN' \
https://mastodon.social/api/v1/accounts/$ACCOUNT_ID/statuses?exclude_replies=true&exclude_reblogs=true&limit=10
)
echo $STATUSES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment