Instagram实时更新没有回应

NO Response from Instagram Real Time Update

本文关键字:回应 实时更新 Instagram      更新时间:2024-01-27

我正在instagram API中进行实时更新。

当我在instagram上点击回调url和hash标签时。它还给了我

{
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);

有了这个,我得到了集线器挑战,但响应数据是空的。通过集线器更改,我向instagram发出了GET请求。当我上传一张带有订阅哈希标签的照片时,instagram没有回应。

有人能说出我缺了什么吗。

http://instagram.com/developer/realtime/

我还没有完全理解你的代码,但我认为你走错了步骤。

当您向"https://api.instagram.com/v1/subscriptions/",Instagram服务器没有立即回答。相反,他向你的回调url发出了一个GET请求。你的代码应该收到这个GET请求,读取querystring参数"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 String:

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由回调url描述。