logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

Tweet on Raspberry Pi via Twython! (Part 1)

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at February 14, 2019
Categories
  • Raspberry Pi
Tags
How to Tweet via Twython by linking Raspberry Pi and Twitter.
Tweet on Raspberry Pi via Twython

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.

What Is Twython?

Twython Official Site

Twython – Twython 3.6.0 documentation

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.

  Twitter libraries — Twitter Developers

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.

Installing Twython

Steps to install Twython
Fig 1.

Getting Started with the Twitter API

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.

1
<em>sudo apt-get update</em>
1
<em>sudo apt-get upgrade</em>

Next, implement the “Twython” install icon.

1
<em>sudo pip3 install twython</em>

Doing that will bring up this message:

Install Twython icon with Raspberry Pi
Fig. 2

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.

1
<em>python3 -c "import twython"</em><br>
Install Twython icon with Raspberry Pi
Fig. 3

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.

Preparing a Twitter Account

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.

How to add Twitter application details

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.

 Twitter application details error
Fig. 5

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.

How to register a phone number for your account

How to create my access token
Fig. 6

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.

Where to find my access token
Fig. 7

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.

Setting up Date/Time

“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.

Posting on Twitter!

“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.

Posting on twitter with Twyton
Fig. 8

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.”

1
2
3
4
5
6
01
02 consumer_key &nbsp;= 'ABCDEFGHIJKLKMNOPQRSTUVWXYZ'
03
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.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
01
02  from twython import Twython
03  
04  from auth import (
05
06     consumer_key,
07  
08     consumer_secret,
09  
10     access_token,
11    
12     access_token_secret
13  )
14
15
16  twitter = Twython(
17
18     consumer_key,
       consumer_secret,
       access_token,
       access_token_secret
)
message = "Hello world!"
twitter.update_status(status=message)
print("Tweeted: %s" % message)

With the “twitter.py” window active, press the “F5” key or run “Run -> Run Module” from the top menu.

Your first tweet on Raspberry pi
Fig. 9

The execution screen should then display 「Tweeted:Hello world!」

Hello world tweet
Fig. 10

Let’s try confirming this by accessing Twitter. If “Hello world!” is posted, then that means we’ve succeeded!

Successfully tweeted with Twython in Raspberry Pi
Fig. 11

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.

In Conclusion

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!

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Tweet on Raspberry Pi via Twython! (Part 2)Tweet on Raspberry Pi via Twython! (Part 2)
  • Using Raspberry Pi GPIO Pins With the RPi.GPIO Python LibraryUsing Raspberry Pi GPIO Pins With the RPi.GPIO Python Library
  • Making a Raspberry Pi Twitter Bot Part 1 – Twitter APIMaking a Raspberry Pi Twitter Bot Part 1 – Twitter API
  • Introduction to Raspberry Pi 4: Let’s Start Using the New “Raspberry Pi 4!”Introduction to Raspberry Pi 4: Let’s Start Using the New “Raspberry Pi 4!”
  • Making the GPIO Easier With GPIO ZeroMaking the GPIO Easier With GPIO Zero
  • Making a Raspberry Pi Twitter Bot Part 2 – Reading RSS feedsMaking a Raspberry Pi Twitter Bot Part 2 – Reading RSS feeds
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort