This repository was archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Instagram Realtime API
Sachin Handiekar edited this page Mar 23, 2015
·
2 revisions
Create an InstagramSubscription object -
InstagramSubscription igSub = new InstagramSubscription()
.clientId(clientId)
.clientSecret(clientSecret)
.object(SubscriptionType.TAGS)
.objectId("london")
.aspect("media")
.callback("http://yourcallbackurl/handleSubscription")
.verifyToken("londonTagSubscription");
SubscriptionResponse subscriptionResponse = igSub.createSubscription();
String subscriptionId = subscriptionResponse.getData().getId();
// more fields available in the subscriptionResponse object.
SubscriptionsListResponse subscriptionList = igSub.getSubscriptionList();
List<SubscriptionResponseData> data = subscriptionList.getData();
for (SubscriptionResponseData responseData : data) {
System.out.println("Id : " + responseData.getId());
System.out.println("Object :" + responseData.getObject());
System.out.println("Type : " + responseData.getType());
}
- Deleting all the subscriptions registered with the clientId.
igSub.deleteAllSubscription();
- Delete a subscription by subscriptionId.
igSub.deleteSubscription(subscriptionId);
package org.jinstagram.examples;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author Sachin Handiekar
*/
public class SubscriptionCallback extends HttpServlet {
public static final String HUB_MODE = "hub.mode";
public static final String HUB_CHALLENGE = "hub.challenge";
public static final String HUB_VERIFY_TOKEN = "hub.verify_token";
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
handlePostRequest(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
handleCallbackURLVerification(req, resp);
}
private void handleCallbackURLVerification(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Instagram will send the following parameter in your callback
// http://your-callback.com/url/?hub.mode=subscribe&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.verify_token=myVerifyToken
String hubChallenge = req.getParameter(HUB_CHALLENGE);
String hubVerifyToken = req.getParameter(HUB_VERIFY_TOKEN);
String hubMode = req.getParameter(HUB_MODE);
resp.getWriter().print(hubChallenge);
}
private void handlePostRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Get the json data from the HttpServletRequest
String jsonData = getBody(req);
SubscriptionResponseObject[] subscriptionResponseDataList = SubscriptionUtil.getSubscriptionResponseData(jsonData);
for (SubscriptionResponseObject subscriptionResponseObject : subscriptionResponseDataList) {
// ObjectId is the name of the tag, i.e. #jinstagram
SessionHandler.sendMessage(subscriptionResponseObject.getObjectId());
}
}
private static String getBody(HttpServletRequest request) throws IOException {
String body = null;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
body = stringBuilder.toString();
return body;
}
}