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.
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 **.
The information to be entered after this is as follows.
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.
Also, the app name cannot be used if it is worn with an existing app.
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.
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.
Since the screen will change to the application screen, change the application permissions from ** Edit ** of ** App permissions **.
Since you need ** Write ** privileges to tweet from Tweepy, select ** Read and Write ** or ** Read + Write + Direct Messages **. Don't forget to save.
Next, go to ** Keys and tokens ** on the app screen. 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. 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.
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!
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