LoginSignup
1
4

More than 1 year has passed since last update.

【Pyhon】tweepy で Twitter API V2を使おうとしたらつまずいたのでメモ

Last updated at Posted at 2022-02-08

参考ページ

トラブル1

tweepyのバージョンが自動で最新にならない

pip review --auto ではなぜかtweepy==3.9.0にしかならず、tweepy.Client()が使えなかった

解決

pypiのページに行って、最新リリースのバージョンを調べ、バージョン指定してpip install する

pip install tweepy==4.5.0

トラブル2

Developer Portalに「App permissions」のボタンが見つからない

初期状態では「App permissions」が「read」になっているため、読み込み以外のことをしようとすると「 403 Forbidden」のエラーが出てしまう

解決

「OAuth 1.0a」を有効化することで、「App permissions」の選択ボタンが表示される

image.png

image.png

おまけ:複数画像ツイートのサンプルコード

import tweepy

CK = '**********'
CS = '**********'
AT = '********************'
AS = '********************'

auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)

api = tweepy.API(auth)


# このリスト内の画像を添付(上限4枚)
listImages = ["test_img.png","test_img.png","test_img.png","test_img.png"]

media_ids = []
for image in listImages:
    img = api.media_upload(image)
    media_ids.append(img.media_id)

api.update_status(status="ツイート文", media_ids=media_ids)
1
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
1
4