This adapter provides a pluggable implementation for integrating Mastodon as a messaging platform. It is designed to work with RelaySMS Publisher, enabling users to connect to Mastodon using OAuth2 authentication.
- Python: Version >= 3.8.10
- Python Virtual Environments: Documentation
Install the necessary system packages:
sudo apt install build-essential python3-dev-
Create a virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
. venv/bin/activate -
Install the required Python packages:
pip install -r requirements.txt
You can register a new client application using the CLI's register command. This creates an OAuth2 application on your Mastodon server.
python3 mastodon_cli.py register \
-n "My Mastodon Client" \
-r "https://example.com/callback/ https://localhost:8080/callback/" \
-w "https://example.com"Command Options:
-n, --name: Client application name-r, --redirect-uris: Redirect URIs (space-separated)-w, --website: Client website URL (optional)
Note
The registration command automatically saves your client credentials to credentials.json in the project directory.
After successful registration, you'll get a credentials.json file with your client credentials:
{
"id": "12345",
"name": "My Mastodon Client",
"website": "https://example.com",
"scopes": ["profile", "write:statuses"],
"redirect_uris": ["https://example.com/callback/"],
"vapid_key": "BM4h...XYZ",
"redirect_uri": "https://example.com/callback/",
"client_id": "abcd1234efgh5678",
"client_secret": "wxyz9876abcd1234efgh5678ijkl9012",
"client_secret_expires_at": 0
}Field Descriptions:
id: Unique identifier for your registered applicationname: The display name of your applicationwebsite: Your application's website URLscopes: OAuth2 scopes your application can request (profile access and posting statuses)redirect_uris: Array of authorized redirect URLs for OAuth2 callbacksvapid_key: Vapid key for push notifications (if applicable)redirect_uri: Primary redirect URI (usually the first inredirect_uris)client_id: Your application's unique client identifierclient_secret: Secret key for authenticating your application (keep this secure!)client_secret_expires_at: Expiration timestamp for the client secret (0 means no expiration)
Create or edit the config.ini file to specify the path to your credentials file:
[credentials]
path = ./credentials.jsonNote
Use the --help flag with any command to see the available parameters and their descriptions.
Use the auth-url command to generate the OAuth2 authorization URL.
python3 mastodon_cli.py auth-url -o session.json-o: Save the output tosession.json.
Use the exchange command to exchange the authorization code for tokens and user info.
python3 mastodon_cli.py exchange -c auth_code -o session.json -f session.json-c: Authorization code.-o: Save the output tosession.json.-f: Read parameters fromsession.json.
Use the send-message command to send a message using the adapter.
python3 mastodon_cli.py send-message -f session.json -m "Hello, Mastodon!" -o session.json-f: Read parameters fromsession.json.-m: Message to send.-o: Save the output tosession.json.
Use the revoke command to revoke the OAuth2 token and invalidate the user's session.
python3 mastodon_cli.py revoke -f session.json -o session.json-f: Read token fromsession.json.-o: Update the file by removing the revoked token.
Warning
After revoking a token, the user will need to re-authenticate to use the adapter again. The revoked token will be removed from the output file if specified.