Pythonを使用して自動ツイートを送信しようとしています。
python-twitterとtweepyの2つの異なるパッケージを試しましたが、どちらのパッケージも使用できませんでした。これらのソリューションについては、ここで説明します。
Twitter API:簡単なステータス更新(Python) Djangoからツイートする方法は?
まず、ツイートを送信するためのTwitterアカウントを数時間前に作成したことを明確にする必要があります。dev.twitter.comを使用して、アクセスレベルを「メッセージの読み取り、書き込み、およびダイレクト」に設定し、コンシューマキーとアクセストークンキーを作成しました。
これらのキーを使用して、python-twitterパッケージを使用してTwitterに接続します。The Biebsのツイートにアクセスできるので、少なくとも部分的に機能していることはわかっています。
>> CONSUMER_KEY = 'XXXX'
>> CONSUMER_SECRET = 'XXXX'
>> ACCESS_TOKEN_KEY= 'XXXX'
>> ACCESS_TOKEN_SECRET= 'XXXX'
>> import twitter
>> api = twitter.Api()
>> api = twitter.Api(
>> consumer_key=CONSUMER_KEY,
>> consumer_secret=CONSUMER_SECRET,
>> access_token_key=ACCESS_TOKEN_KEY,
>> access_token_secret=ACCESS_TOKEN_SECRET
>> )
>> statuses = api.GetUserTimeline("justinbieber")
>> for s in statuses:
>> print s.text
http://t.co/q58qksxp its not finished but heres a little part a song I'm working on
sorry http://t.co/Vtu4Lc2Q
video is at 13 percent done uploading
<... Other Inanities from The Bieb Snipped. You get the picture...>
ただし、ツイートを送信しようとすると、「OAuthで認証できませんでした」というエラーが表示されます。
>> api.PostUpdate('Testing Twitter!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.5-py2.7.egg/twitter.py", line 2838, in PostUpdate
data = self._ParseAndCheckTwitter(json)
File "/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.5-py2.7.egg/twitter.py", line 3869, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File "/usr/local/lib/python2.7/dist-packages/python_twitter-0.8.5-py2.7.egg/twitter.py", line 3892, in _CheckForTwitterError
raise TwitterError(data['error'])
twitter.TwitterError: Could not authenticate with OAuth.
誰かが理由を説明できますか?それがうまくいかなかったので、私はtweepyをインストールして試すことにしました。
しかし、使用しようとすると、「無効または期限切れのトークン」というエラーが表示されます。
>> import tweepy
>> def tweet(status):
>> '''
>> updates the status of my twitter account
>> requires tweepy (https://github.com/joshthecoder/tweepy)
>> '''
>> if len(status) > 140:
>> raise Exception('status message is too long!')
>> auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
>> auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
>> api = tweepy.API(auth)
>> result = api.update_status(status)
>> return result
>>
>> result = tweet('Testing Twitter!')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in tweet
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.0-py2.7.egg/tweepy/binder.py", line 185, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.0-py2.7.egg/tweepy/binder.py", line 168, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{u'message': u'Invalid or expired token', u'code': 89}]
ここでの解決策は何ですか?Twitterのキーは間違っていますか?