python related packages. Please refer to other sites for installation methods.
python 2.7.9
pip 1.3.1
In python 2.7.6, the following error occurred when communicating with the REST API.
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:79:
InsecurePlatformWarning: A true SSLContext object is not available.
This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail.
For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
After reading the description on the above site, I recommend upgrading to 2.7.9. So I upgraded to 2.7.9.
Install OAuth authentication library
$ pip install requests requests_oauthlib
Source code
twitter_api_test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import json
from requests_oauthlib import OAuth1Session
oauth_info = {
'consumer_key': os.environ.get('TWITTER_API_CONSUMER_KEY'),
'consumer_secret': os.environ.get('TWITTER_API_CONSUMER_SECRET'),
'access_token': os.environ.get('TWITTER_API_ACCESS_TOKEN'),
'access_token_secret': os.environ.get('TWITTER_API_ACCESS_TOKEN_SECRET')
}
oauth = OAuth1Session(
oauth_info['consumer_key'],
oauth_info['consumer_secret'],
oauth_info['access_token'],
oauth_info['access_token_secret']
)
url = 'https://api.twitter.com/1.1/search/tweets.json'
params = {
'q': u'#python',
'lang': 'ja',
'result_type': 'recent',
'count': '15'
}
res = oauth.get(url, params=params)
if res.status_code != 200:
print '[ERROR] Unexpected code: %d' % res.status_code
exit(1)
tweets = json.loads(res.text)
for tweet in tweets['statuses']:
print '-----'
print tweet['text']
Run
$ export TWITTER_API_CONSUMER_KEY=xxx #For the following 4 lines, use the one obtained by "Get Access Token".
$ export TWITTER_API_CONSUMER_SECRET=yyy
$ export TWITTER_API_ACCESS_TOKEN=zzz
$ export TWITTER_API_ACCESS_TOKEN_SECRET=aaa
$ python twitter_api_test.py
Like this.
-----
RT @_liongarden:Recruitment of Python engineers! Let's create a cloud marketplace together by Lion Garden Co., Ltd. https://t.co/REcRr7RdkM #wantedly #python #nodejs #angularjs
-----
RT @_liongarden:Recruitment of Python engineers! Let's create a cloud marketplace together by Lion Garden Co., Ltd. https://t.co/REcRr7RdkM #wantedly #python #nodejs #angularjs
-----
Shannon Lab holds regular python study sessions at the local Hachioji. Beginners of programming can also participate. Search for "Python study group Hachioji". You can register from ATND.#python
-----
RT @_liongarden:Recruitment of Python engineers! Let's create a cloud marketplace together by Lion Garden Co., Ltd. https://t.co/REcRr7RdkM #wantedly #python #nodejs #angularjs
-----
(Omitted below)
Recommended Posts