我正在使用Instagram API中的实时更新。
当我使用回调网址和哈希标记点击Instagram时。它让我回来了
{
meta: {
code: 200
}-
data: [0]
}
这是标签订阅的代码,
HttpURLConnection connection = null;
String requestUrl= "https://api.instagram.com/v1/subscriptions";
String subscriptionUrl="client_secret=xxx&client_id=xx&object=tag&aspect=media&object_id=tagName&verify_token=xx&callback_url=http://mycallbackUrl.htm";
connection = (HttpURLConnection) new URL(requestUrl).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(subscriptionUrl.getBytes().length));
connection.connect();
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(subscriptionUrl);
wr.flush();
wr.close();
InputStream stream = connection.getInputStream();
InputStreamReader reader= new InputStreamReader(stream);
BufferedReader bufferStream = new BufferedReader(reader);
String readResponse;
StringBuffer response = new StringBuffer();
while((readResponse = bufferStream.readLine()) !=null)
{
response.append(readResponse);
}
bufferStream.close();
这是我与HUB CHANLLENGE的要求,
String fetchContent;
String responsetUrl= "https://api.instagram.com/v1/subscriptions?client_secret=8a3c35a329ad44fa82c0f006fc48a2d7&client_id=f90b48725f844e61bb2672d83011fcd8&hub.challenge="+hubchallenge;
HttpURLConnection connections= (HttpURLConnection) new URL(responsetUrl).openConnection();
connections.setRequestMethod("GET");
BufferedReader br= new BufferedReader(new InputStreamReader(connections.getInputStream()));
while((fetchContent = br.readLine()) !=null)
log.info("the hubchallenge response::"+fetchContent);
有了这个,我得到了集线器挑战,但响应数据是空的。随着集线器挑战,我向Instagram发出了GET请求。 当我上传带有订阅哈希标签的图片时,没有来自Instagram的回复。
任何人都可以告诉我遗失的内容。
答案 0 :(得分:0)
http://instagram.com/developer/realtime/
我还没有完全理解你的代码,但我认为你遵循了错误的步骤。
当您向" https://api.instagram.com/v1/subscriptions/"发出POST请求时,Instagram服务器不会立即回答。相反,他向你的回调网址发出GET请求。您的代码应该接收此GET请求,读取查询字符串参数" hub.challenge",并将其作为简单字符串返回。 Instagram服务器将收到您的回复,看到hub.challenge与他发送的相同,并创建您的订阅。然后他将回复您的初始POST请求,该请求一直在等待。
我也在Java上实现了这个,我使用了两个servlet。
此servlet方法从文本框中读取标记并尝试订阅:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
String tag = request.getParameter("tag");
String url = "https://api.instagram.com/v1/subscriptions";
try
{
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "client_id=" + URLEncoder.encode(Constants.clientId, "UTF-8") + "&client_secret="
+ URLEncoder.encode(Constants.clientSecret, "UTF-8") + "&object=tag&aspect=media&object_id=" + URLEncoder.encode(tag, "UTF-8")
+ "&verify_token=" + URLEncoder.encode("my_password", "UTF-8") + "&callback_url=" + URLEncoder.encode(Constants.callbackUrl, "UTF-8");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer result = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
result.append(inputLine);
}
in.close();
JSONObject json = new JSONObject(result.toString());
System.out.println(json);
}
catch (Exception exc)
{
System.out.println(exc.getMessage());
}
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp");
try
{
dispatcher.forward(request, response);
}
catch (IOException e)
{
e.printStackTrace();
}
}
在另一个servlet上,此代码从Instagram服务器获取GET请求并返回hub.challenge字符串:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String mode = request.getParameter("hub.mode");
String challenge = request.getParameter("hub.challenge");
String verifyToken = request.getParameter("hub.verify_token");
// you should check that these values are ok
PrintWriter writer = response.getWriter();
writer.write(challenge);
writer.flush();
}
订阅成功后,我使用此代码收听订阅事件:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
BufferedReader body = request.getReader();
String inputLine;
StringBuffer result = new StringBuffer();
while ((inputLine = body.readLine()) != null)
{
result.append(inputLine);
}
body.close();
String str = result.toString();
try
{
JSONArray jsArray = new JSONArray(str);
if (jsArray != null && jsArray.length() > 0)
{
for (int index = 0; index < jsArray.length(); index++)
{
JSONObject json = jsArray.optJSONObject(index);
System.out.println(json);
}
}
}
catch (JSONException exc)
{
exc.printStackTrace();
}
}
此代码必须与之前的servlet在同一个servlet上,这是您的回调网址所描述的代码。