我正在尝试联系 Google Books API 并执行标题搜索,这只需要一个公共(public) API key 而不需要 OAUTH2。我得到的只是以下错误:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "accessNotConfigured",
"message": "Access Not Configured"
}
],
"code": 403,
"message": "Access Not Configured"
}
}
在谷歌搜索了几个小时后,似乎许多其他人也遇到了同样的问题,但使用的是其他 Google API。到目前为止我做了什么:
- 在我的开发者控制台中注册了一个项目
- 启用 Books API
- 签署我的申请以获取 SHA1 证书编号
- 选择在我的开发者控制台中获取 Android 的公共(public) API key
- 将以下字符串粘贴到公共(public) API key 表单中,以获取 key :“SHA1 number;com.package”,不带引号
- 将生成的 key 复制粘贴到我的代码中。
代码如下:
private void callGoogleBooks(){
String key = MY_KEY;
String query = "https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=" + key;
Log.d("google books", callApi(query));
}
public String callApi(String query){
HttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(query);
HttpResponse httpResponse = null;
try{
httpResponse = httpClient.execute(getRequest);
} catch(UnsupportedEncodingException e){
Log.d("ERROR", e.getMessage());
} catch(ClientProtocolException e){
Log.d("ERROR", e.getMessage());
} catch (IOException e){
Log.d("ERROR", e.getMessage());
}
if(httpResponse != null){
try{
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null){
sb.append(line + "\n");
}
is.close();
String responseString = sb.toString();
return responseString;
} catch (Exception e){
Log.d("ERROR", e.getMessage());
}
}
return null;
}
- 有没有明显的错误?我是否需要以不同方式格式化或打包我的请求?
- 我需要向 list 文件中添加任何内容吗?
- 在生成 API 公钥时指定包时,是否需要指定与我的应用程序结构中相同的包名称?我在某处读到它必须是唯一的,但将其更改为不太可能重复的内容会导致相同的错误。
错误显然与“usageLimits”有关,但在我的测试项目中,我什至不接近每天允许的 1000 次调用的 1%。
我还尝试实现 Google Books Java Sample不使用上面的代码,得到相同的错误信息。我也试过禁用和重新启用 Books API,但没有成功。
提前致谢。
最佳答案
这对我有用
String link = "https://www.googleapis.com/books/v1/volumes?q="+params;
InputStream is = null;
try
{
int timeoutConnection = 10000;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(timeoutConnection);
con.setReadTimeout(timeoutConnection);
con.setRequestProperty("key", "API_KEY");
if(con.getResponseCode() != HttpURLConnection.HTTP_OK){
publishProgress("Error conneting.");
}
is=con.getInputStream();
}
关于android - Google Books API 403 访问未配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926166/