Posting tweets with python

1.First of all

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

2. Get the API key to access 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.

3. Actually posting to twitter

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

Posting tweets with python
Collecting tweets with Python
Search twitter tweets with python
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Exclude tweets containing URLs with tweepy [Python]
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python
Python programming with Atom