[PYTHON] Install tweepy with pip and use it for API 1.1

The environment is Windows 64bit Python 2.7.3 (I don't think it matters)

1. Install pip

Run at command prompt (or terminal) > easy_install pip

2. Install tweepy

Run at command prompt > pip install tweepy Once installed, use the Python interpreter (interactive) > import tweepy Let's check.

3. Use tweepy

The version of tweepy that can be installed with pip is still API 1.0. There is a 1.1 compatible version of Github for a long time, but pip is easy to install, so if you can do it with pip, you want to do it with pip! (Git weak) When writing the script, it is assumed that twitter CK / CS and access token data have already been acquired. Creating an instance for operating the API with tweepy is as follows

python


import tweepy

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth_handler=auth, api_root='/1.1', secure=True)

By putting'/1.1' in the constructor of tweepy.API class with api_root attribute, you can basically support API1.1.

python


api.update_status(raw_input())

You can post like this.

Cases where this alone does not work

There are some APIs whose endpoint structure has changed when changing from API 1.0 to 1.1. This time, take the destroy_status function that deletes tweets as an example. In the tweepy.API class, the destroy_status function is declared as follows:

python


from tweepy.binder import bind_api

destroy_status = bind_api(
        		path = '/statuses/destroy.json',
        		method = 'DELETE',
        		payload_type = 'status',
        		allowed_param = ['id'],
        		require_auth = True
				)

bind_api is a class that wraps the functions that tweepy uses to hit the API (maybe) In API1.1, the statuses / destroy endpoint has changed to /statuses/destroy/{id}.json. This time I tried as follows

python


import tweepy
from tweepy.binder import bind_api

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth_handler=auth, api_root='/1.1', secure=True)

destroy_status_1_1 = bind_api(
        		path = '/statuses/destroy/{id}.json',
        		method = 'POST',
        		payload_type = 'status',
        		allowed_param = ['id'],
        		require_auth = True
				)
destroy_status_1_1(api, raw_input()) #API instance and status_pass id

You have now written a script that supports API 1.1.

Recommended Posts

Install tweepy with pip and use it for API 1.1
Install Python 2.7.9 and Python 3.4.x with pip.
Install pip and pandas with Ubuntu or VScode
How to install Cascade detector and how to use it
Error with pip install
Use pip with MSYS2
Install scikit.learn with pip
How to use Service Account OAuth and API with Google API Client for python
Install selenium on Mac and try it with python
Until you can install blender and run it with python for the time being
Use Trello API with python
Use Twitter API with Python
Use pip with Jupyter Notebook
When moss with pip install
Install github repository with pip
Use subsonic API with python3
Easy to use Nifty Cloud API with botocore and python
Install CaboCha in Ubuntu environment and call it with Python.
I got an error when pip install tweepy on macOS Sierra, so I dealt with it
I cried with `pip install --upgrade pip` with Bad md5 hash for package, but it got better
Dealing with PermissionError [Error 1] for pip install -U pip on macOS Sierra
Permission denied with pip install pyopenssl
[Python] What is pip? Explain the command list and how to use it with actual examples
Manipulate colors with pip install color
How to install and use Graphviz
Install pip in Serverless Framework and AWS Lambda with Python environment
[TensorFlow] If you want to run TensorBoard, install it with pip
Install lp_solve on Mac OS X and call it with python.
How to install mysql-connector with pip3
[Python Windows] pip install with Python version
Install easy_install and pip on windows
How about creating a virtual environment with Anaconda and doing pip install?
Install ROS and ROS module for Roomba on RaspberryPi3 and try to run it
I got an SSL related error with pip install, so I solved it
Search for Twitter keywords with tweepy and write the results to Excel
Put MeCab binding for Python with pip on Windows, mac and Linux
Edit Slide (PowerPoint for Google) with Python (Low-cost RPA case with Google API and Python)
Don't use your username and password to register with PyPI. Use API tokens
Use MeCab and neologd with Google Colab
SSL certificate related with pip install? Error
About package management with conda and pip
Use DeepL with python (for dissertation translation)
How to install and use pandas_datareader [Python]
Solution for pip install error [Python] [Mac]
Use xticks () for pyplot and set_xticklabels () for Axes.
selenium: wait for element with AND / OR
AWS ~ For those who will use it ~
Use Jupyter Lab and Jupyter Notebook with EC2
I want to pip install with PythonAnywhere
Use PIL and Pillow with Cygwin Python
Install by specifying the version with pip
scipy stumbles with pip install on python 2.7.8
Install python on xserver to use pip
Day 64 pip install tensorflow and 2.0 is here.
Easy! Use gensim and word2vec with MAMP.
Array buffer object for use with Cython
Use Python and MeCab with Azure Functions
I can't install the package with pip.
Automatic follow-back using streaming api with Tweepy
Let's install box2d-py with Windows 10 environment pip
If you try to install Python2 pip after installing Python3 pip and it is rejected