Writing an Android Twitter Client with Python

Learn:

  • Access Twitter API

Requirements

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

read_tweet.py

import tweepy # 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: print tweet.text

Send Tweet

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.

  1. 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.
  2. Get the access tokens - see dev.twitter.com/docs/auth/tokens-devtwittercom
  3. Modify the send_tweet.py with your tokens

send_tweet.py

import tweepy # 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) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) user = api.verify_credentials() # checks that your authentication is OK print user.screen_name # 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

twitter.zip