The environment is Windows 64bit Python 2.7.3 (I don't think it matters)
Run at command prompt (or terminal)
> easy_install pip
Run at command prompt
> pip install tweepy
Once installed, use the Python interpreter (interactive)
> import tweepy
Let's check.
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.
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