せかいや

いまいるここを、おもしろく http://sekai-in-the-box.appspot.com/

python-twitter-0.8.5 はTwitterAPI1.1に対応してない

python-twitter-0.8.5(※1)をインストールし実行するもエラー。

■API1 ->1.1を使ってねとエラー表示

>>> api = twitter.Api()
>>> statuses = api.GetUserTimeline('iroironashi')
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\python_twitter-0.8.5-py2.7.egg\twitter.py"
, line 2721, in GetUserTimeline
data = self._ParseAndCheckTwitter(json)
File "C:\Python27\lib\site-packages\python_twitter-0.8.5-py2.7.egg\twitter.py"
, line 3869, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File "C:\Python27\lib\site-packages\python_twitter-0.8.5-py2.7.egg\twitter.py"
, line 3894, in _CheckForTwitterError
raise TwitterError(data['errors'])
twitter.TwitterError: [{u'message': u'The Twitter REST API v1 is no longer activ
e. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.',
u'code': 68}]

実際にtwitter.py の中を見ると、確かにAPI1のまま。

if base_url is None:
self.base_url = 'https://api.twitter.com/1'
else:
self.base_url = base_url

ということで、最新のv1.0を再度DLしてインストール。
ダウンロードページにも、1.1対応してると記載されていた。
ううー。最初からこっち落としておけば良かった!

Python Twitter v1.0 now works with v1.1 of the Twitter API

python-twitter-0.8.5 のアンインストール
サイト(※2)を参考に。

1)easy-install.pthの確認⇒ python-twitter-0.8.5 の行を削除

./python-twitter-0.8.5.egg

2)setup.py の確認⇒削除対象らしきものはなし

3)eggディレクトリの削除

4)アンインストール結果の確認

・インポート文でエラー。いいぞー

C:\Windows\system32>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named twitter
>>> print sys.path


・システムパスの記載も消えている。いいぞー

>>> import sys
>>> print sys.path
['', 'C:\\Python27\\lib\\site-packages\\distribute-0.6.49-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\httplib2-0.8-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\oauth2-1.5.211-py2.7.egg', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']

■再インストール

C:\Windows\system32>cd C:\PythonLib\python-twitter-1.0

C:\PythonLib\python-twitter-1.0>python setup.py install
running install
running bdist_egg

Using c:\python27\lib\site-packages\httplib2-0.8-py2.7.egg
Finished processing dependencies for python-twitter==1.0

■呼び出し成功!

C:\PythonLib\python-twitter-1.0>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (
Type "help", "copyright", "credits" or "license" for more informat
>>> import twitter
>>> api = twitter.Api('hoge','hoge' …
>>> statuses = api.GetUserTimeline('hoge')
>>> print statuses


※1
https://code.google.com/p/python-twitter/

※2:pythonモジュールのアンインストール
http://d.hatena.ne.jp/kokiya/20080920/1221931695