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() {
  
}

Posting media with a status update

This demonstrates how you can include binary files in your sketch file to uploads and parsing JSON responses.

import com.francisli.processing.restclient.*;

RESTClient uploadClient, client;
HttpRequest mediaRequest, statusRequest;

void setup() {
  size(640, 480);
  
  //// draw a pretty picture
  background(0);
  noStroke();
  float r = random(255);
  float g = random(255);
  float b = random(255);
  for (int diameter = 640; diameter > 0; diameter -= 40) {
    fill(r, g, b, 30);
    ellipse(width/2, height/2, diameter, diameter);
  }
  
  //// save current contents of frame to a file
  save("sketch.png");

  uploadClient = new RESTClient(this, "upload.twitter.com");
  uploadClient.logging = true;
  uploadClient.useSSL = true;
  uploadClient.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 = 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 files = new HashMap();
  files.put("media", "sketch.png");
  mediaRequest = uploadClient.POST("/1.1/media/upload.json", null, files);
}

void responseReceived(HttpRequest request, HttpResponse response) {
  println(response.statusCode + ": " + response.statusMessage);
  if (request == mediaRequest) {
    JSONObject obj = response.getContentAsJSONObject();
    String mediaId = obj.getString("media_id_string");
    
    HashMap params = new HashMap();
    params.put("status", "Look at my sketch!");
    params.put("media_ids", mediaId);
    statusRequest = client.POST("/1.1/statuses/update.json", params);
  } else if (request == statusRequest) {
    println(response.getContentAsString());
  }
}

void draw() {
  
}
Clone this wiki locally