android - Google Books API 403 访问未配置

标签 android google-api google-api-java-client google-books

我正在尝试联系 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。到目前为止我做了什么:

  1. 在我的开发者控制台中注册了一个项目
  2. 启用 Books API
  3. 签署我的申请以获取 SHA1 证书编号
  4. 选择在我的开发者控制台中获取 Android 的公共(public) API key
  5. 将以下字符串粘贴到公共(public) API key 表单中,以获取 key :“SHA1 number;com.package”,不带引号
  6. 将生成的 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();
}

来自这个线程:Google Books API for Android - Access Not Configured

关于android - Google Books API 403 访问未配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926166/

相关文章:

google-api - 调用 users().list().execute() 时,Google Directory API 返回 Not Authorized

android - 使用 windowSoftInputMode ="adjustPan"隐藏操作栏并调整调整大小收缩布局

javascript - 给定 DataTable 时,Google Charts draw() 方法类型错误

Android:TextView 中的多种样式静态

javascript - 如何使用简单的 ajax jQuery 对 DialogFlow V2 进行 http 调用?

google-apps-script - muteHttpExceptions = true 抛出身份验证失败

java - google-api-client 和 google-api-services 之间的版本兼容性

google-api-java-client 与 gdata-java-client 中的 Youtube 支持

android - 如何在 android 的 Canvas 上创建一个按钮并将该按钮链接到要显示/隐藏的图像?

android - 未找到 protoc-gen-javanano