1

我试图从 freebase 请求大量数据。但我收到错误消息“HTTP 响应代码:403”。以前有没有人遇到过类似的问题?

这是我的代码

私有静态最终字符串 service_url = " https://www.googleapis.com/freebase/v1/mqlread ";

public static void main(String[] args) 
{
    try
    {
        String cursor = "";
        String urlStr_initial = service_url + "?query=" + URLEncoder.encode(getQuery(), "UTF-8") + "&cursor";
        URL url = new URL(urlStr_initial);
        List<Freebase> list = new ArrayList<Freebase>();
        Freebase f_b;
        do
        {
            HttpURLConnection  url_con = (HttpURLConnection)url.openConnection();
            url_con.addRequestProperty("User-Agent", "Mozilla/4.76");

            StringBuilder str_builder = new StringBuilder();        
            BufferedReader reader = new BufferedReader(new InputStreamReader(url_con.getInputStream()));

            String line;
            while((line = reader.readLine()) != null)
            {
                str_builder.append(line);
            }
            String response = str_builder.toString();
            JSONObject j_object = new JSONObject(response);

            if(j_object.has("result"))
            {
                JSONArray j_array = j_object.getJSONArray("result");
                for(int i = 0; i < j_array.length(); i++)
                {
                    JSONObject j_o = j_array.getJSONObject(i);
                    if(j_o.has("id") == true && j_o.has("name"))
                    {
                        String id = j_o.getString("id");
                        String name = j_o.getString("name");
                        System.out.println("ID: " + id + "   /   Name:" + name);
                        f_b = new Freebase(id, name);
                        if(f_b != null)
                        {
                            list.add(f_b);
                        }
                        else
                        {
                            System.out.println("Null value in Freebase");
                        }
                    }
                }
            }
            else
            {
                System.out.println("There is no \"result\" key in JASON object");
            }

            if(j_object.has("cursor"))
            {
                cursor = j_object.get("cursor").toString();
            }
            else
            {
                cursor = "false";
            }

            String urlStr = urlStr_initial + "=" + cursor;
            url = new URL(urlStr);
        }while( !cursor.equalsIgnoreCase("false"));

        if(list != null)
        {
            TextFile tf = new TextFile();
            tf.writeToFile(list);
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

private static String getQuery()
{
    return "[{"
            + "\"name\": null,"
            + "\"id\": null,"
            + "\"type\":\"/people/person\","
            + "\"limit\": 500"
            + "}]";
}
4

1 回答 1

1

你没有说什么是“大”,但 API 不是为批量下载而设计的。您应该为此使用数据转储。

HTTP 响应代码中通常包含更详细的错误消息。例如,如果它显示 403 - Forbidden - API key required,这意味着您没有包含您的 API 密钥(我没有看到您在代码中包含它的位置)。如果显示 403 - Forbidden - quota exceeded,则表示您已超出请求配额(您可以在 API 控制台上查看剩余的配额)。

于 2013-04-14T14:59:41.357 回答