-
Notifications
You must be signed in to change notification settings - Fork 5
Examples: Twitter API
This example demonstrates basic usage of the GET method to request data from a server.
Details: https://dev.twitter.com/docs/api/1/get/statuses/public_timeline
import com.francisli.processing.http.*;
HttpClient client;
void setup() {
client = new HttpClient(this, "api.twitter.com");
client.GET("/1/statuses/public_timeline.json");
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.getContentAsString());
}
void draw() {
}
This example demonstrates using the JSONObject to extract data from a JSON response.
Details: https://dev.twitter.com/docs/api/1/get/statuses/public_timeline
import com.francisli.processing.http.*;
HttpClient client;
void setup() {
client = new HttpClient(this, "api.twitter.com");
client.GET("/1/statuses/public_timeline.json");
}
void responseReceived(HttpRequest request, HttpResponse response) {
//// check for HTTP 200 success code
if (response.statusCode == 200) {
JSONObject results = response.getContentAsJSONObject();
//// we know Twitter returns a JSON array, so use size() and get() to access elements
for (int i = 0; i < results.size(); i++) {
//// get the next element in the array
JSONObject tweet = results.get(i);
//// each element in the JSON array is an object/dictionary of metadata,
//// so we use get() with String keys to fetch data
//// we know text is a String value
String text = tweet.get("text").stringValue();
//// we know user is another JSON object/dictionary
JSONObject user = tweet.get("user");
//// we know screen_name is a String value in the user object
String screen_name = user.get("screen_name").stringValue();
println(screen_name + ": " + text);
}
} else {
//// output the entire response
println(response.getContentAsString());
}
}
void draw() {
}
This example demonstrates passing query parameters to a GET request.
Details: https://dev.twitter.com/docs/api/1/get/search
import com.francisli.processing.http.*;
HttpClient client;
void setup() {
client = new HttpClient(this, "search.twitter.com");
//// 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("/search.json", params);
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.getContentAsString());
}
void draw() {
}
This example demonstrates setting up OAuth authorization and using the POST method to send data. 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.
Details: https://dev.twitter.com/docs/api/1/post/statuses/update
import com.francisli.processing.http.*;
HttpClient client;
void setup() {
client = new HttpClient(this, "api.twitter.com");
//// using SSL is optional, but recommended
client.useSSL = true;
//// turn on OAuth request signing
client.useOAuth = true;
//// set up OAuth signing parameters
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/statuses/update.json", params);
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.statusCode + ": " + response.statusMessage);
println(response.getContentAsString());
}
void draw() {
}
This example demonstrates sending the contents of a file with a POST request.
Details: https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
import com.francisli.processing.http.*;
HttpClient client;
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");
client = new HttpClient(this, "upload.twitter.com");
//// using SSL is optional, but recommended
client.useSSL = true;
//// turn on OAuth request signing
client.useOAuth = true;
//// set up OAuth signing parameters
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", "Posting from Processing with an image!");
HashMap files = new HashMap();
files.put("media[]", "sketch.png");
client.POST("/1/statuses/update_with_media.json", params, files);
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.statusCode + ": " + response.statusMessage);
println(response.getContentAsString());
}
void draw() {
}