1
1

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.

【2023年8月】tweepyで動画付きツイートをする方法

Posted at

TL;DR

v1とv2を両方使い、アップロード、ツイートを行う(らしい)

以下サンプル

import tweepy

# 認証
api_key = ""
api_secret = ""
access_token = ""
access_token_secret = ""

def get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret) -> tweepy.API:
    """Get twitter conn 1.1"""

    auth = tweepy.OAuth1UserHandler(api_key, api_secret)
    auth.set_access_token(
        access_token,
        access_token_secret,
    )
    return tweepy.API(auth)


def get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret) -> tweepy.Client:
    """Get twitter conn 2.0"""

    client = tweepy.Client(
        consumer_key=api_key,
        consumer_secret=api_secret,
        access_token=access_token,
        access_token_secret=access_token_secret,
    )

    return client


client_v1 = get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret)
client_v2 = get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret)

media_path = r"C:~~.mp4"

media = client_v1.media_upload(filename=media_path, media_category="tweet_video")
media_id = media.media_id

client_v2.create_tweet(text="Tweet text", media_ids=[media_id])

こちら必ず指定してあげてください。

media_category="tweet_video"

誰かのお役に立てれば...

参考

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?