Writing an Android Twitter Client with Python
- The example programs can be run from the emulator and smartphone.
- Point your smartphone or emulator browser to m.smartphonedaq.com to download and install the Tweepy API. Tweepy is Twitter library for python for accessing the Twitter.com API. Supports OAuth, covers the entire API, and streaming API. The Twitter API that comes with Py4A does not support Oauth. If you use RemoteControl, the Tweepy API must be installed on your PC.
Read the Timeline
Do try out the read twitter example first as it does not require authentication and can be run as is.
# NEAsg is the user_id or screen_name of the NEA Singapore
public_tweets = tweepy.api.user_timeline(id="NEAsg")
for tweet in public_tweets:
You need to have a twitter account if you want to send twits. The send twit program requires authentication information and you must modify the example program before running. We are using the authentication method to access the API from your own account.
- Create a new application at dev.twitter.com. Go ahead and fill in the required fields (the website URL can be a placeholder). Set the application to read/write access.
- Get the access tokens - see dev.twitter.com/docs/auth/tokens-devtwittercom
- Modify the send_tweet.py with your tokens
# modify the following before running
CONSUMER_KEY = 'Consumer key'
CONSUMER_SECRET = 'Consumer secret'
ACCESS_KEY = 'Access token'
ACCESS_SECRET = 'Access token secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
api = tweepy.API(auth)
user = api.verify_credentials()
# checks that your authentication is OK
# to avoid spamming, please change the text
api.update_status('tweepying from my android')
Do observe the anti-spamming rules of twitter. It may prevent your status from being updated.
For more information on the teepy.api, please read the Tweepy API documentation.
Scan the script to your smartphone using the Test EMANT380 app or download to PC/Emulator
- Launch Test EMANT380 app. (Bluetooth must be enabled)
- Tap Scan Files. (ZXing barcode scanner app must be installed)
- Scan the barcode and the script(s) to your SL4A list.