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