Skip to content

Examples: Twitter API

Francis Li edited this page Sep 4, 2017 · 6 revisions

OAuth authentication

All calls to the Twitter API require registration and authentication. First, you will need to set up an application with Twitter and generate a user token for your account (see Setting up a new app with Twitter).

Important: Do not share your consumer secret and user token secret, or they can be used by anyone to post and abuse the system with your account! In this example, I’ve put placeholder String values for you to insert your own authorization data, but if you plan to share your code you should consider storing them in a separate file. Also, do not distribute exported applets or applications, as these data can be easily extracted from the exported files.

Fetching a Timeline

This example demonstrates basic usage of the GET method to request data from a server.

Details: https://dev.twitter.com/rest/reference/get/statuses/home_timeline

import com.francisli.processing.restclient.*;

RESTClient client;

void setup() {
  client = new RESTClient(this, "api.twitter.com");
  client.useSSL = true;
  client.useOAuth = true;
  client.oauthConsumerKey = "insert your app consumer key here";
  client.oauthConsumerSecret = "insert your app consumer secret here";
  client.oauthAccessToken = "insert your user access token here";
  client.oauthAccessTokenSecret = "insert your user access token secret here";
  client.GET("/1.1/statuses/home_timeline.json");
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.getContentAsString());
}

void draw() {
  
}

Searching for Tweets

This example demonstrates passing query parameters to a GET request.

Details: https://dev.twitter.com/rest/reference/get/search/tweets

import com.francisli.processing.restclient.*;

RESTClient client;

void setup() {
  client = new RESTClient(this, "api.twitter.com");
  client.useSSL = true;
  client.useOAuth = true;
  client.oauthConsumerKey = "insert your app consumer key here";
  client.oauthConsumerSecret = "insert your app consumer secret here";
  client.oauthAccessToken = "insert your user access token here";
  client.oauthAccessTokenSecret = "insert your user access token secret here";
  //// instantiate a new HashMap
  HashMap params = new HashMap();
  //// put key/value pairs that you want to send in the request
  params.put("q", "processing");
  client.GET("/1.1/search/tweets.json", params);
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.getContentAsString());
}

void draw() {
  
}

Posting a Status Update

Details: https://dev.twitter.com/rest/reference/post/statuses/update

import com.francisli.processing.restclient.*;

RESTClient client;

void setup() {
  client = new RESTClient(this, "api.twitter.com");
  client.useSSL = true;
  client.useOAuth = true;
  client.oauthConsumerKey = "insert your app consumer key here";
  client.oauthConsumerSecret = "insert your app consumer secret here";
  client.oauthAccessToken = "insert your user access token here";
  client.oauthAccessTokenSecret = "insert your user access token secret here";
  //// set up POST params
  HashMap params = new HashMap();
  params.put("status", "This is a tweet from a Processing sketch!");
  client.POST("/1.1/statuses/update.json", params);
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.statusCode + ": " + response.statusMessage);
  println(response.getContentAsString());
}

void draw() {
  
}

Clone this wiki locally