LoginSignup
3
3
はじめての記事投稿

tweepyを使いTwitter API V2に画像を投稿しよう!

Last updated at Posted at 2023-06-18

Tweepyを使いTwitter API V2で画像を投稿する方法

TwitterAPIv2.py
import tweepy

# Twitter API credentials
consumer key = "xxxxxxxxxxxxxxxxxxxx"
consumer secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
access_token_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Authenticate Twitter API
auth = tweepy. OuthHandler ( consumer_key, consumer_secret)
auth. set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API (auth)
client = tweepy.Client (
consumer_key=consumer_key, consumer_secret=consumer_secret, access_token-access_token, access_token_secret=access_token_secret)

# Attach image and message to tweet
image_path = './sample. jpg'# Specify image file path
message = 'sample message'# Specify message
media = api. media_upload (filename=image_path)
client. create_tweet (text=message, media_ids= [media.media_id])
3
3
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
3
3