Try using the Twitter API

Application registration

  1. Go to here and select "Create New App"
  2. Enter the required information and select "Create your Twitter application"

Get Access Token

  1. Select the "Keys and Access Tokens" tab on the screen after the previous operation
  2. Select "Create my access token" (at the bottom of the screen)
  3. Information is output near the same bottom.

A little test with Python

environment

python related packages. Please refer to other sites for installation methods.

/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

Test code

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

result

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

Try using the Twitter API
Try using the Twitter API
Try using the PeeringDB 2.0 API
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Try using the BitFlyer Ligntning API in Python
Try using the DropBox Core API in Python
Try drawing a social graph using Twitter API v2
Try to delete tweets in bulk using Twitter API
Post to your account using the API on Twitter
Try using Janus gateway's Admin API
Specifying the date with the Twitter API
Try using Pleasant's API (python / FastAPI)
Try using Python argparse's action API
Try using the Python Cmd module
Access the Twitter API in Python
I tried using the checkio API
Try hitting the Twitter API quickly and easily with Python
Create a real-time auto-reply bot using the Twitter Streaming API
Tweet Now Playing to Twitter using the Spotify API. [Python]
For the time being, try using the docomo chat dialogue API
Try using Tkinter
Try using docker-py
I tried using Twitter api and Line api
Try using the web application framework Flask
Try using the $ 6 discount LiDAR (Camsense X1)
Try using the HL band in order
Try using cookiecutter
Try using PDFMiner
Try using the camera with Python's OpenCV
Try the Microsoft Cognitive Services Face API
Try using geopandas
Try using Dropbox API v2 with Go
Try using Selenium
Try using scipy
Try cluster analysis using the K-means method
Create an application using the Spotify API
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try hitting the YouTube API in Python
Try using tf.metrics
Play with puns using the COTOHA API
Try using PyODE
Try hitting the Spotify API in Django.
Record custom events using the Shotgun API
I tried using the BigQuery Storage API
Tweet regularly with the Go language Twitter API
I checked the library for using the Gracenote API
Hit the Web API using requests Example: Flickr
Python: Try using the UI on Pythonista 3 on iPad
Try using the Chinese morphological analysis engine jieba
I tried using the Google Cloud Vision API
Try using the Python web framework Tornado Part 1
Try using LINE Notify for the time being
Try using the collections module (ChainMap) of python3
Try using kabu station API of kabu.com Securities
Try using the Python web framework Tornado Part 2
Try accessing the YQL API directly from Python 3
Try using ChatWork API and Qiita API in Python