twitter APIで起きていたこと

2023年11月3日

■適用範囲
twitter APIを利用した配信

  • twitter APIを使ってwordcloudをcron定期送信していたら、いつの日からか”key error”でPythonプログラムが停止していました。
    対処方法の1例を紹介します。

#!/usr/bin/env python
# coding: utf-8

import requests_oauthlib

SEARCH_URL = 'https://api.twitter.com/1.1/search/tweets.json’
QUERY = '********’

API_KEY = '****************************’
API_SECRET_KEY = '************************************’
ACCESS_TOKEN = '****************************************************’
ACCESS_TOKEN_SECRET = '******************************************’

session = requests_oauthlib.OAuth1Session(
client_key=API_KEY, client_secret=API_SECRET_KEY,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_TOKEN_SECRET)

params = {'q’: QUERY, 'count’: 100, 'tweet_mode’: 'extended’}
res = session.get(SEARCH_URL, params=params)
content = res.json()

import re
texts = []
for status in content['statuses’]: #ここでkey errorが発生
text = status['full_text’]

  • Jupyter notebookを使って、contentの内容にてmessage errorを確認。メッセージは以下のとおり。

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

  • 理由までは不明ですが、twitterアカウントロックの状態になっていたようでアプリで手動にて再認証し復旧です。(暫定対処ですが)

■参考にさせていただいたURL
https://tech.unifa-e.com/entry/2021/03/11/082632
https://www.mk-mode.com/blog/2017/03/15/about-twitter-lock/

 

今まで無料で利用できたtwitter API v2は2023年に有料化となりました。無料ライセンスでは動きません。

{'errors': [{'message': 'You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product',
   'code': 453}]}

\ 最新情報をチェック /