目前我正在使用 Google Books API。
我写了一个简单的请求:
public static String request(String url)
{
DefaultHttpClient client = new DefaultHttpClient();
client.getConnectionManager().getSchemeRegistry().register(
new Scheme("SSLSocketFactory", SSLSocketFactory.getSocketFactory(), 443));
HttpGet method = new HttpGet(url);
String data = null;
try
{
BasicHttpResponse response = (BasicHttpResponse) client.execute(method);
data = EntityUtils.toString(response.getEntity());
}
catch (Exception e)
{
System.out.println("IOException in HTTPSRequest.request: " + e.toString());
}
return data;
}
之后我除了一个JSonArray:
static final String baseURL = "https://www.googleapis.com/books/v1/volumes?key=" + APIKey + "&q=";
public static String getURL(String searchstring)
{
return baseURL + searchstring;
}
public static JSONObject getJSONObjectFromString(String jsonString)
{
JSONParser parser = new JSONParser();
JSONObject object = null;
try
{
object = (JSONObject) parser.parse(jsonString);
}
catch (Exception e)
{
System.out.println("ParseException in GoogleBooksHelper.getJSONObjectFromString");
}
return object;
}
public static JSONArray getItemsAsJSONArray(String jsonString)
{
JSONObject object = getJSONObjectFromString(jsonString);
JSONArray items = (JSONArray) object.get("items");
return items;
}
但是现在的问题是,每次我开始一个请求时,我都会得到以下信息:
{"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "accessNotConfigured",
"message": "Access Not Configured"
}
],
"code": 403,
"message": "Access Not Configured"
}
}
但我不知道为什么。在 Api 控制台中,我启用了 Book api,我生成了一个带有项目名称的新 Api 密钥。