【问题标题】:Google Books API for Android - Access Not Configured适用于 Android 的 Google Books API - 未配置访问权限
【发布时间】:2014-01-16 11:34:51
【问题描述】:

我正在使用 google books API KEY for android 通过我的 android 应用程序加载图书数据。但我收到下面列出的错误。

然而, - 如果我从我的 API CONSOLE 中删除证书(例如,使 API KEY 对所有应用程序都可接受),则查询有效。尽管我输入的 {SHA1};{package name} 信息是正确的。 - 如果我将 API KEY 用于浏览器,这将有效。

因此,我能理解的是,我无法在 httpclient 方法中将 KEY 作为 url 的一部分发送。可能是我需要通过标题或其他方式发送。但我找不到方法。

有人可以帮忙吗?

代码:

String link = "https://www.googleapis.com/books/v1/volumes?key=MY_KEY&q="+query+"&projection=full&langRestrict=en&maxResults=1";
HttpGet get = new HttpGet(link);
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
HttpClient httpclient = new  DefaultHttpClient(httpParameters);
HttpResponse response = httpclient.execute(get);
HttpEntity entity = response.getEntity();

查询:

https://www.googleapis.com/books/v1/volumes?key=MY_API_KEY&q=intitle:'The+Old+Man+And+The+Sea'+inauthor:'Ernest+Hemingway'&projection=full&langRestrict=en&maxResults=1

结果:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured"
   }
  ],
  "code": 403,
  "message": "Access Not Configured"
 }
}

【问题讨论】:

    标签: android google-api google-books


    【解决方案1】:

    它可能关心的人...我在标头中发送我的 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();
                }
    

    【讨论】:

    • 谢谢!这实际上是错误的记录,因为 Books API 的文档说开发密钥很可能作为 GET-Param 给出。但它似乎只用作标题参数。这是一个 SO 帖子如何使用 Volley 设置标题:stackoverflow.com/questions/17049473/…
    【解决方案2】:

    在标题中提供密钥与根本不提供密钥相同。检查您的开发者 api 控制台,您将不会看到使用该 api 密钥的成功查询。所以简而言之,标头中的 API 密钥并不能解决问题。如果你不想按照 google 的规则来玩,就直接在没有 key 的情况下进行查询。

    【讨论】:

      【解决方案3】:

      您可以使用HttpGet 获得与接受的答案相同的结果:

      String API_KEY = "..."; // Google Books API Public key
      String ISBN = "...";
      String bookSearchString = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + ISBN;
      HttpClient bookClient = new DefaultHttpClient();
      try
      {
          //get the data
          HttpGet bookGet = new HttpGet(bookSearchURL);
          bookGet.addHeader("key", API_KEY);
          HttpResponse bookResponse = bookClient.execute(bookGet);
          StatusLine bookSearchStatus = bookResponse.getStatusLine();
          if (bookSearchStatus.getStatusCode() == 200)
          {
              //we have a result
              HttpEntity bookEntity = bookResponse.getEntity();
      
              ...
      
          }
      } catch (Exception e)
      {
          e.printStackTrace();
      }
      

      【讨论】:

        【解决方案4】:

        请注意,insecure 可以不受限制地使用 Google API 密钥!

        显然,如果您因访问 API 数据而被收费,那么您不希望有人反编译您的 APK,找到您的 API 密钥,然后开始在他们自己的应用中使用它。

        How to hide your API Key in Android?

        但这还不够。您可以将 API 密钥存储在某个地方,以便其他人也可以找到它。

        要保护您的 Android 应用的 Google 云 API 密钥,您必须将其限制为只能在您的应用中使用。请参阅here 了解更多信息。

        在限制密钥之后,您必须在发送给 Google 的每个请求的标头中包含您的 android 应用程序包名称和 SHA-1 证书指纹。 How to do?

        这就是你所需要的! :D

        【讨论】:

          猜你喜欢
          • 2014-01-22
          • 2013-01-06
          • 2014-09-23
          • 1970-01-01
          • 2017-02-11
          • 2014-02-08
          • 1970-01-01
          • 2015-08-13
          • 2015-04-02
          相关资源
          最近更新 更多