Java Instagram实时更新没有响应
我正在instagram API中进行实时更新 当我用回拨url和哈希标签点击instagram时。它回报了我Java Instagram实时更新没有响应,java,javascript,Java,Javascript,我正在instagram API中进行实时更新 当我用回拨url和哈希标签点击instagram时。它回报了我 { meta: { code: 200 }- data: [0] } 这是我的标签订阅代码 HttpURLConnection connection = null; String requestUrl= "https://api.instagram.com/v1/subscriptions"; String subscriptionUrl="client_secret=xxx&am
{
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的GET请求
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);
通过这个,我得到了hub挑战,但是响应数据是空的。通过hub挑战,我向instagram发出了GET请求。
当我上传一张带有订阅的散列标签的图片时,instagram没有响应
谁能告诉我我错过了什么
我还没有完全理解您的代码,但我认为您执行了错误的步骤
当您向“”发出POST请求时,Instagram服务器不会立即响应。相反,他向您的回调url发出GET请求。您的代码应该接收这个GET请求,读取querystring参数“hub.challenge”,并将其作为简单字符串返回。Instagram服务器将收到您的响应,查看hub.challenge是否与他发送的相同,然后创建您的订阅。然后他会回复你一直在等待的最初发帖请求
我也在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();
}
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException
{
BufferedReader body=request.getReader();
字符串输入线;
StringBuffer结果=新的StringBuffer();
而((inputLine=body.readLine())!=null)
{
结果。追加(输入行);
}
body.close();
字符串str=result.toString();
尝试
{
JSONArray jsArray=新JSONArray(str);
if(jsArray!=null&&jsArray.length()>0)
{
对于(int index=0;index
这段代码必须和以前一样位于同一个servlet上,即回调url描述的servlet