LoginSignup
1
0

More than 1 year has passed since last update.

【Python】pythonでtweetしてみた

Last updated at Posted at 2022-04-29

概要

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からツイートテスト")

実行結果

実行した結果は以下のようになります。
tweet.PNG

Twitter関係のAPIはめんどくさい印象でしたが、意外と簡単にできました。
次は定期的にツイートできるようにしてみようかなと思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0