Tweet from python with Twitter Developer + Tweepy

Click here to register for Twitter Developer: The story of the difficulty in registering for Twitter Developer for the second time If you are registering for the first time, you should not have any trouble.

Get an access token

Project creation

After logging in to the account you applied for on Twitter Developer, access Dashboard. Since there is no Project yet and it is in a blank state, create a project from ** + Create Project **. dashbord

The information to be entered after this is as follows.

  1. Project name (free entry)
  2. Your position (selection type)
  3. Project description (free entry)
  4. App name (free input)

It is necessary to decide the application name separately from the project name. Example: Name the project "TestProject", name the app "MyTestApp", etc.

My position is the same option as when registering with Twitter Developer. Choose the position that best suits your project. choose

Also, the app name cannot be used if it is worn with an existing app. error

When the application name is decided safely, the screen will change to the following screen. You can check this information later, so let's move on to the settings from ** App settings ** first. accesstoken

Permission settings

Click App settings to move to the page of the created project. From here, make settings to get the token used in Tweepy. First, click the gear in the app created by Apps. project

Since the screen will change to the application screen, change the application permissions from ** Edit ** of ** App permissions **. apps

Since you need ** Write ** privileges to tweet from Tweepy, select ** Read and Write ** or ** Read + Write + Direct Messages **. Don't forget to save. permission

Get tokens

Next, go to ** Keys and tokens ** on the app screen. key_and_tokens You can check ** API key & secret ** as many times as you like from ** View Keys **. However, due to security issues, it will not be possible to confirm after about a year, so be sure to leave a note at hand. apikey Note that ** Access token & secret ** cannot be checked again after it is generated once. If you forget / lose a note, you can only invalidate or regenerate it. 13.png

Tweet from python with Tweepy

From here, move to python. Install tweepy with ** pip install tweepy **.

tweet.py


import tweepy

Consumer_key = 'Obtained API key'
Consumer_secret = 'Obtained API Key secret'
Access_token = 'Obtained Access token'
Access_secret = 'Obtained Access token secret'

#Create an OAuth handler
auth = tweepy.OAuthHandler(Consumer_key, Consumer_secret)
#Tell the OAuth handler the access token
auth.set_access_token(Access_token, Access_secret)
#Create an API
api = tweepy.API(auth_handler=auth)

#Tweet
api.update_status('Tweeting with API.')

only this!

Isn't it dangerous to write a solid token?

If you give this program to Git, the tokens that should be kept secret will be lost all over the world. However, when the code becomes complicated, you also want to manage Git ....

As one of the solutions, let's write a token in the ** environment variable! There is **.

In the case of Windows, environment variables can be added from the settings. For mac, write the following 4 lines in .bashrc if you are using bash, or in .zshrc if you are using zsh. (Xxxx is changed to your own key.'(Quotation) and "(double quotation) are not required.) After that, reflect the settings with ** source ~ / .bashrc ** or ** source ~ / .zshrc ** on the terminal.

shell:.bashrc/.zshrc


export TWITTER_CK=xxxx
export TWITTER_CS=xxxx
export TWITTER_AT=xxxx
export TWITTER_AS=xxxx

If it can be reflected in the environment variables safely, rewrite the previous program as follows.

tweet2.py


import tweepy
import os

Consumer_key = str(os.getenv('TWITTER_CK'))
Consumer_secret = str(os.getenv('TWITTER_CS'))
Access_token = str(os.getenv('TWITTER_AT'))
Access_secret = str(os.getenv('TWITTER_AS'))

#Create an OAuth handler
auth = tweepy.OAuthHandler(Consumer_key, Consumer_secret)
#Tell the OAuth handler the access token
auth.set_access_token(Access_token, Access_secret)
#Create an API
api = tweepy.API(auth_handler=auth)

#Tweet
api.update_status('Tweeting with API.')

It's safe to publish with this!

Recommended Posts

Tweet from python with Twitter Developer + Tweepy
[Memo] Tweet on twitter with python
Collecting information from Twitter with Python (Twitter API)
Create wordcloud from your tweet with python3
Twitter graphing memo with Python
Get Twitter timeline with python
Use Twitter API with Python
Collecting information from Twitter with Python (morphological analysis with MeCab)
Crawl the URL contained in the twitter tweet with python
Tweet with image in Python
Search twitter tweets with python
With skype, notify with skype from python!
[Tweepy] Re: Twitter Bot development life starting from zero # 1 [python]
Collecting information from Twitter with Python (MySQL and Python work together)
Get celebrity tweet history from twitter
Call C from Python with DragonFFI
Install Python from source with Ansible
[Lambda] [Python] Post to Twitter from Lambda!
Post multiple Twitter images with python
Run Aprili from Python with Orange
I liked the tweet with python. ..
Call python from nim with Nimpy
Easily post to twitter with Python 3
Get data from Twitter using Tweepy
Read fbx from python with cinema4d
Receive textual data from mysql with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Tweet using the Twitter API in Python
Business efficiency starting from scratch with Python
Decrypt files encrypted with openssl from python with openssl
Working with Azure CosmosDB from Python Part.2
Post from another account with Twitter API
Image acquisition from camera with Python + OpenCV
Getting started with Dynamo from Python boto
Tweet analysis with Python, Mecab and CaboCha
Try calling Python from Ruby with thrift
Scraping from an authenticated site with python
Use C ++ functions from python with pybind11
Let's make a Twitter Bot with Python!
I want to tweet on Twitter with Python, but I'm addicted to it
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
FizzBuzz with Python3
Tweet regularly with the Go language Twitter API
Csv output from Google search with [Python]! 【Easy】
Scraping with Python
Extract text from PowerPoint with Python! (Compatible with tables)
Make a Twitter trend bot with heroku + Python
Statistics with python
Scraping with Python
Image processing from scratch with python (5) Fourier transform
I made a Twitter fujoshi blocker with Python ①
Twilio with Python
Integrate with Python
Wrap C with Cython for use from Python
Play with 2016-Python
sql from python
AES256 with python
~ Tips for Python beginners from Pythonista with love ① ~
Tested with Python