I want to tweet on Twitter with Python, but I'm addicted to it

When I heard that I wanted to tweet on Twitter automatically, I tried to make it a little, but I was addicted to it

Missing CONSUMER_KEY

If you look at various sites and search, there are 4 items required to tweet from python.

CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET

I need it, but no matter how I look at it, there is no CONSUMER_KEY and CONSUMER_SECRET!

スクリーンショット 2020-10-10 16.10.27.png

However, I can't find it anyway, so when I search

CONSUMER_KEY is API KEY CONSUMER_SECRET seems to be good with API_key_secret.

It is a code that you often see if you search, but I changed the variable to the following code. I think this is easier to understand.

twitter.py


import json
from requests_oauthlib import OAuth1Session

#Key and Token here
API_KEY = 'XXXXXXX'
API_KEY_SECRET = 'XXXXXXX'
ACCESS_TOKEN = 'XXXXXXX'
ACCESS_TOKEN_SECRET = 'XXXXXXX'

twitter = OAuth1Session(API_KEY, API_KEY_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

url = "https://api.twitter.com/1.1/statuses/update.json"
print(twitter)

tweet = "This is a test tweet from Python\r line break\r\n line break\n line break"  #Tweet content

params = {"status" : tweet}

req = twitter.post(url, params = params) #Tweet here

if req.status_code == 200: #success
    print("Succeed!")
else: #error
    print("ERROR : %d"% req.status_code) 

I can't tweet with a 401 error

Even if you execute with the above code after taking API_KEY

ERROR : 401

I can't tweet at all. This case was a setting omission. As per 401, there was no authority.

First of all, since the first setting is read only, I changed the setting so that it can also be written.

スクリーンショット 2020-10-12 20.20.33.png

However, the 401 error continues.

Why couldn't you?

Maybe after changing to read & write settings Regenerate ACCESS_KEY again.

スクリーンショット 2020-10-12 20.21.00.png

It seems that you cannot write while using ACCESS_KEY, which can only be read unless you acquire it again.

If the 401 error still doesn't work, try getting it several times and assigning it to a variable.

Reference material

https://prog-masaki.com/tweet-python/

Recommended Posts

I want to tweet on Twitter with Python, but I'm addicted to it
I want to do it with Python lambda Django, but I will stop
I want to AWS Lambda with Python on Mac!
I want to debug with Python
[Memo] Tweet on twitter with python
I'm an amateur on the 14th day of python, but I want to try machine learning with scikit-learn
I want to analyze logs with Python
I want to play with aws with python
I want to use MATLAB feval with python
I want to make a game with Python
Use Python from Java with Jython. I was also addicted to it.
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
The file edited with vim was readonly but I want to save it
I want to send Gmail with Python, but I can't because of an error
I want to convert horizontal text to vertical text and post it on Twitter etc.
I want to handle optimization with python and cplex
I tried to implement Minesweeper on terminal with python
I was addicted to scraping with Selenium (+ Python) in 2020
I want to inherit to the back with python dataclass
I want to work with a robot in python.
[ML Ops] I want to do multi-project with Python
I want to run a quantum computer with Python
I want to be able to analyze data with Python (Part 3)
[IOS] GIF animation with Pythonista3. I was addicted to it.
I want to specify another version of Python with pyvenv
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
[Python] I want to use the -h option with argparse
When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
I want to set up a GUI development environment with Python or Golang on Mac
I made a program to collect images in tweets that I liked on twitter with Python
I don't tweet, but I want to use tweepy: just display the search results on the console
I liked the tweet with python. ..
I want to do ○○ with Pandas
I'm addicted to Python 2D lists
Easily post to twitter with Python 3
I couldn't import the python module with VSCODE, but I could do it on juoyterlab, so I'm talking about finding the cause.
Life game with Python [I made it] (on the terminal & Tkinter)
Don't write Python if you want to speed it up with Python
I want to use a wildcard that I want to shell with Python remove
I want to know the weather with LINE bot feat.Heroku + Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
What I was addicted to with json.dumps in Python base64 encoding
I want to output the beginning of the next month with Python
I want to know if you install Python on Mac ・ Iroha
I want to do a full text search with elasticsearch + python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to summarize everyone's remarks on slack with wordcloud (Python)
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
Tweet from python with Twitter Developer + Tweepy
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere