4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめての記事投稿

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])
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?