I have had a few requests here and there for a Processing sketch to access the Instagram API and retrieve photographs.
Here is code for a very simple Processing sketch that retrieves and saves the first 10 photographs of an image based on provided hashtag. In the JSONObject, make sure to swap in your tag and client id.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void setup() { size(640, 640); JSONObject instagram = loadJSONObject("https://api.instagram.com/v1/tags/YOURTAGHERE/media/recent?client_id=YOURCLIENTIDHERE"); JSONArray photos = instagram.getJSONArray("data"); println(photos); for (int i = 0; i < photos.size(); i++) { String url1 = photos.getJSONObject(i).getJSONObject("images").getJSONObject("standard_resolution").getString("url"); PImage img = loadImage(url1, "jpg"); img.save("output/result"+i+".jpg"); image(img, 0, 0, 640, 640); } } void draw() { noLoop(); } |
TANK YOU SO MUCH FOR THIS CODE!
But, I got this Error Message. Could you help me?
java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.instagram.com/v1/tags/YOURTAGHERE/media/recent?client_id=YOURCLIENTIDHERE
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
at processing.core.PApplet.createInputRaw(PApplet.java:7152)
at processing.core.PApplet.createInput(PApplet.java:7098)
at processing.core.PApplet.createReader(PApplet.java:6904)
at processing.core.PApplet.loadJSONObject(PApplet.java:6080)
at sketch_190523a.setup(sketch_190523a.java:19)
at processing.core.PApplet.handleDraw(PApplet.java:2425)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.instagram.com/v1/tags/YOURTAGHERE/media/recent?client_id=YOURCLIENTIDHERE
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
at processing.core.PApplet.createInputRaw(PApplet.java:7145)
… 7 more
The file “https://api.instagram.com/v1/tags/YOURTAGHERE/media/recent?client_id=YOURCLIENTIDHERE” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
NullPointerException
No problem, you would need to replace a few things in the URL:
https://api.instagram.com/v1/tags/YOURTAGHERE/media/recent?client_id=YOURCLIENTIDHERE
replace “YOURTAGHERE” with the hashtag you are interested in.
replace “YOURCLIENTIDHERE” with your client ID which you would obtain from Instagram by registering with them as a developer:
https://www.instagram.com/developer/
That being said, it looks like Instagram is phasing out the capabilities of their API so I can’t vouch for this code to still be working.