概要
pythonでtweetしてみた。
前提条件
Pythonが既にインストールされていること。
API(tweepy)のインストール
コンソールを開いて下記のコマンドを実行する。
pip install tweepy
APIキーとアクセスキー、トークンの取得
下記のURLを参考に認証に必要なキー情報を取得する。
Pythonでツイートを実行
Essentialだと下記のエラーが出たためtweepy + Twitter API V2でツイートのプログラムを参考にしました。ありがとうございます。
53 - you currently have essential access which includes access to twitter api v2 endpoints only. if you need access to this endpoint, you’ll need to apply for elevated access via the developer portal. you can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
実際に実行したプログラムは下記になります。
import tweepy
# Twitter Deverloper Portalで取得
#取得したAPI Key
ck = 'xxxxxxxxxxxx'
#取得したAPI Key Secret
cs = 'xxxxxxxxxxxx'
#取得したAccess Token
at = 'xxxxxxxxxxxx'
#取得したAccess Token Secret
ats = 'xxxxxxxxxxxx'
client = tweepy.Client(consumer_key=ck, consumer_secret=cs, access_token=at, access_token_secret=ats)
client.create_tweet(text="pythonからツイートテスト")
実行結果
Twitter関係のAPIはめんどくさい印象でしたが、意外と簡単にできました。
次は定期的にツイートできるようにしてみようかなと思います。