Hit Mastodon's API in Python

Recently I'm using Mastodon as my main SNS, so I thought I'd try making something with a bot, so instead of a memorandum

Obtaining an access token

Get an access token to hit the API. From Mastodon's home ** User Settings → Development → New App ** You should get to this page. image.png Enter the name of the app on this page and press ** Send ** at the bottom The name you entered earlier is displayed, so if you press it

** Client key Client secret Access token **

I think that is displayed. All you need after this is an access token. You don't need to remember this page as you can always see it.

Try Toot

You can toot with the following code.

toot.py


import requests
headers = {'Authorization': 'Bearer xxxxxxxxxxxxxxxxx'}    #Enter the access token you obtained in xxxxxxxxxxxxxxxxx.
host = "https://example.com/api/v1/statuses"    #example.Change com to the domain of your instance.
toot = ""   #toot text
content = toot.encode()    #Doesn't work without encoding
data = {'status': cotent}
post = requests.post(host, "headers"=headers, "data"=data)
print(post.status_code)

The only library you need is requests, so it's very easy to toot. It is not good for security to write the Access token directly in the code, so please use it properly. It seems that it doesn't work well unless the text toot is encoded when tooting, so I had a hard time. If 200 is output with this, you can safely toot.

Create a sentence you want to toot in a textbook, bring it, and turn it with cron to complete the bot.

Recommended Posts

Hit Mastodon's API in Python
Hit the Sesami API in Python
Hit the web API in Python
Evernote API in Python
C API in Python 3
Hit the Firebase Dynamic Links API in Python
Blender Python API in Houdini (Python 3)
Getting the arXiv API in Python
Hit a command in Python (Windows)
[Python] Hit the Google Translation API
Hit the Etherpad-lite API with Python
Create Gmail in Python without API
Quickly implement REST API in Python
Access the Twitter API in Python
An easy way to hit the Amazon Product API in Python
Quadtree in Python --2
Python in optimization
CURL in python
Mouse operation using Windows API in Python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Try using the Wunderlist API in Python
Meta-analysis in Python
Try using the Kraken API in Python
Unittest in python
Hit Zabbix API (1.8, 2.0)
[FX] Hit oanda-API in Python using Docker
Epoch in Python
Tweet using the Twitter API in Python
Discord in Python
Get Google Fit API data in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Hit the New Relic API in Python to get the server status
Programming in python
Get Youtube data in Python using Youtube Data API
Plink in Python
Constant in python
Quickly try Microsoft's Face API in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Try hitting the YouTube API in Python
Puyo Puyo in python
python in virtualenv
PPAP in Python