Instagram API integration with HTTP library

So I'm working on a project that pulls from instagram but I'm having some problems getting the api working with processing. I found some source code to work with here so I tried it out (just testing the waters with basically the same code to see if I could get the API working), but it doesn't work for me and I can't figure out why.

It's using an http library by francisli, which I have successfully downloaded and installed from here. When I run my code (see below), I can see that getGrams() is successfully being called, as every second I get this message in the console (with the proper access token, of course):

HttpClient: Connecting to api.instagram.com on port 443
HttpClient: GET https://api.instagram.com:443/v1/tags/cats/media/recent.json?count=1&access_token=ACCESS_TOKEN HTTP/1.1

The framerate is also printed in the console, so I know the if statement is working properly and such. However, nothing is being printed from the void responseReceived() function. According to the http library documentation, the void responseReceived() function should be called whenever processing receives a response from my GET request, so I can only assume that processing isn't receiving any responses.

I don't think this is because of any access problems, since when I follow the link from my GET request (https://api.instagram.com:443/v1/tags/cats/media/recent.json?count=1&access_token=ACCESS_TOKEN), it's showing the relevant JSON just fine. So either Instagram isn't sending it to Processing, or Processing isn't getting it from Instagram. Or, instagram's api has changed recently, or the http library is not compatible with my version of processing (which is 2.2.1), or somethings just wrong with my code.

So my question is, how can I get the Instagram API to work? I honestly have no idea what I'm doing wrong and no idea where to even begin to try to figure this out, so some help would be much appreciated.

Thank you in advance.

import com.francisli.processing.http.*;

PImage userphoto;
PImage profilepicture;

String username;
String tag;
String[] tagStrings;

String comment;

com.francisli.processing.http.HttpClient client;

void setup() {
  size(900, 800);
  smooth();

  client = new com.francisli.processing.http.HttpClient(this, "api.instagram.com");
  client.useSSL = true;
}

void getGrams() {
    //// instantiate a new HashMap
  HashMap params = new HashMap();
  //// put key/value pairs that you want to send in the request
  params.put("access_token", "1722917717.cbb3637.c9d399ea75b24dad9afe5f8b52e186db");
  params.put("count", "1");
  client.GET("/v1/tags/cats/media/recent.json", params);
}

void responseReceived(com.francisli.processing.http.HttpRequest request, com.francisli.processing.http.HttpResponse response) {
  println(response.getContentAsString());
  println("RESPONSE RECEIVED");

  //// we get the server response as a JSON object
  com.francisli.processing.http.JSONObject content = response.getContentAsJSONObject();

  //// get the "data" value, which is an array
  com.francisli.processing.http.JSONObject data = content.get("data");

  //// get the first element in the array
  com.francisli.processing.http.JSONObject first = data.get(0);

  //// the "user" value is another dictionary, from which we can get the "full_name" string value
  println(first.get("user").get("full_name").stringValue());

  //// the "caption" value is another dictionary, from which we can get the "text" string value
  //println(first.get("caption").get("text").stringValue());

  //// get profile picture
  println(first.get("user").get("profile_picture").stringValue());

  //// the "images" value is another dictionary, from which we can get different image URL data
  println(first.get("images").get("standard_resolution").get("url").stringValue());

  com.francisli.processing.http.JSONObject tags = first.get("tags");
  tagStrings = new String[tags.size()];
  for (int i = 0; i < tags.size(); i++) {
    tagStrings[i] = tags.get(i).stringValue();
  }

  comment = first.get("caption").get("text").stringValue();
  username = first.get("user").get("full_name").stringValue();

  String profilepicture_url = first.get("user").get("profile_picture").stringValue();
  profilepicture = loadImage(profilepicture_url, "png");

  String userphoto_url = first.get("images").get("standard_resolution").get("url").stringValue();
  userphoto = loadImage(userphoto_url, "png");

}


void draw() {
  background(255);
  imageMode(CENTER);


  if(frameCount % 100 == 0){
    println(frameCount);
    getGrams();
  }

  if (profilepicture != null) {
    image(profilepicture, 60, 70, 90, 90);
  }
  imageMode(CENTER);
  if (userphoto != null) {
    image(userphoto, width/2, height/2.25, 550, 550);
  }

  textSize(20);
  if (username != null) {
    text(username, 110, 115);
    fill(0);
  } else if (username == null) {
    text("null username", 110, 115);
    fill(0);
  }

  textSize(20);
  if (comment != null) {
    text(comment, 15, 700, 550, 100);
    fill(0);
  }


}

Answers

  • edited March 2015

    Very interesting project! If you could post the access key I would love to help figure out the delema.

  • edited July 2015

    I have the same issue. Anyone knows why responseReceived() is not working?

Sign In or Register to comment.