This article was translated to English, and was originally published for deviceplus.jp.
Today, I’ll talk about linking up Raspberry Pi and Twitter through “Twython.” I’ve tried linking up Twitter in the past, but it’s harder than it seems! This time, we’ll be following the Raspberry Pi Official Site Document. We’ll be using the 2017-11-29 release of “Raspbian Stretch with desktop” OS version with a “Raspberry Pi2 Model B” for this article.
Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs
「Twython」is a library for Python for using Twitter API.
twython by @ryanmcgrath — Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs. Supports all v1.1 endpoints, including dynamic functions so users can make use of endpoints not yet in the library.
The official Twitter also introduces this.
Let’s proceed. Click on the left menu when you’d like to switch the page, as the URL doesn’t change.
The second from the top, “What you will need,” describes how to install “Twython for Python 3.”
As always, start by updating the system.
<em>sudo apt-get update</em>
<em>sudo apt-get upgrade</em>
Next, implement the “Twython” install icon.
<em>sudo pip3 install twython</em>
Doing that will bring up this message:
Checking on the “pip3 list” command shows that “twython (3.4.0)” was confirmed, so it looks like it was already installed.
Finally, execute a confirmation command to see if you have everything you need.
<em>python3 -c "import twython"</em><br>
If no errors come up, that means preparations are complete!
At the end of the page, there’s a warning stating that a verifiable phone number is necessary for using Twitter API. If you don’t register a phone number, you won’t be able to complete the Twitter registration on the application, so make sure to have this prepared beforehand.
The third item, “Create a Twitter account,” details how to make a Twitter account. For new accounts, follow the Twitter Official Site to set up an account. When using an existing account, go on to the fourth page.
The “Create a Twitter application” details how to make a Twitter application.
Sign into a Twitter account on this page Twitter Application Managementand click the “Create New App” button.
Next, input a “Name,” “Description,” and “Website.” Filling in something for “Website” is necessary, but since I won’t be taking this account public, I put in my own Twitter URL instead. After agreeing to the terms and conditions, click the “Create your Twitter application” button.
If you don’t have a phone number registered to your Twitter account, the following error message is going to appear.
When that happens, refer to the page below to begin registering a phone number.
After you’re finished registering, click the “Keys and Access Tokens” button.
“Consumer Key (API Key)” and “Consumer Secret (API Secret)” are values that Twython needs to verify.
First, let’s confirm the access level.
If you have it as “Read and write,” that’s OK. I had that setting on by default when I was setting up, but you can change it anytime you want by clicking on the “modify app permissions” link.
Next is making an access token. Click the “Create my access token” button at the bottom of the screen.
Under “Your Access Token,” “Access Token” and “Access Token Secret” will be displayed. Same as “Consumer Key (API Key)” and “Consumer Secret (API Secret)” from before, these values are needed when using Twython.
“Set the system date/time” on the fifth page is the date and time setting, so let’s adjust the date and time on the Raspberry Pi’s setting.
“Send a tweet from Python” on the sixth page will finally give us the chance to write a program! Select “Programming”-“Python 3(IDEL)” from the menu and open editor.
The initial display screen will look like this, but if it stays like this, each line will be executed line by line, so let’s create a new one starting with [File] – [New File]. Line up 「Consumer Key (API Key)」「Consumer Secret (API Secret)」「Access Token」「Access Token Secret」 in the order below and save in a file called “auth.py.”
02 consumer_key = 'ABCDEFGHIJKLKMNOPQRSTUVWXYZ'
04 consumer_secret = '1234567890ABCDEFGHIJKLMNOPQRSTUVXYZ'
access_token = 'ZYXWVUTSRQPONMLKJIHFEDCBA'
access_token_secret = '0987654321ZYXWVUTSRQPONMLKJIHFEDCBA'
Python needs to be aware that the indentation by leading spaces and tab characters affects the program.
(Lines indented with the same number are considered as blocks)
I saved this file on the “/home/pi/” directory. I think when you log in as a pi user, this directory will be selected as default. Next, we’ll be typing out the program for the Twitter post. Again, create a new file on [File]-[New File] and write down the program, as seen below. Then, save the file as “twitter.py” in the same directory as “auth.py.”
02 from twython import Twython
04 from auth import (
16 twitter = Twython(
message = "Hello world!"
print("Tweeted: %s" % message)
With the “twitter.py” window active, press the “F5” key or run “Run -> Run Module” from the top menu.
The execution screen should then display 「Tweeted:Hello world!」
Let’s try confirming this by accessing Twitter. If “Hello world!” is posted, then that means we’ve succeeded!
If you try to continue posting the same Tweet, this error message is going to come up. When that happens, try changing the content of the “message” and word order to get it to work.
For part 1 of “Twython,” we’ll stop here at the character word order for the Tweets.
When I worked on Twitter related project before, I had direct access to Twitter’s API, but I thought the difficulty level got much lower by using the library.
I think that “Twython” with Python as a base is easy to introduce to any electronics that uses parts. Posting images, searching tweets… We’ll be talking about those in the next article!
Next time, we’ll be going over pages 7-9 of the document and trying out a variety of different tweets!