Since I usually use twitter, I want to post and get the timeline using a program, so to do that I will leave it here as a memorandum.
The following contents are described here. B. Obtaining an API key to access twitter B. Actually posting to twitter
Since twitter exposes the API, we will access it using it. Get the API key by referring to the following site. Twitter API key acquisition procedure: I'm still a programmer, but what?
I want four items here: Consumer Key, Consumer Secret, Access Token, and Access Token Secret.
From here, I will actually post tweets from the program using python. Perform in the following environment.
You also need the following libraries
First, import the library.
twitter_access.py
from requests_oauthlib import OAuth1Session
import json
from urllib import request
Next, assign to a variable to use the API key obtained earlier, and create an Oauth session using the API key.
twitter_access.py
keys = {
"CK":'Consumer Key',
"CS":'Consumer Secret',
"AT":'Access Token',
"AS":'Access Token Secret',
}
sess = OAuth1Session(keys["CK"], keys["CS"], keys["AT"], keys["AS"])
Access the fixed URL using this session and get method, You can get tweets and timelines by using the post method. You can hit various APIs by changing this fixed URL. More information can be found at the Twitter Developer Documentation (https://dev.twitter.com/rest/reference). The process of actually tweeting is as follows.
twitter_access.py
url = "https://api.twitter.com/1.1/statuses/update.json"
params = {"status":"Hello World!"}
#Hello World!Part is actually tweeted
req = sess.post(url, params = params)
if req.status_code == 200:
print ("OK")
else:
print ("Error")
This will tweet Hello World !. Next, write the acquisition of the timeline.
Recommended Posts