【问题标题】:Accessing Books API through AppEngine fails with error code 400通过 AppEngine 访问 Books API 失败,错误代码 400
【发布时间】:2014-07-01 14:55:55
【问题描述】:

从 Google AppEngine 应用程序访问 Google Books API 时出现以下错误。

使用了服务器应用程序的 API 密钥。

但是如果你在eclipse本地运行应用程序就没有问题了。

{ “代码”:403, “错误”:[{ “域”:“全球”, "message" : "无法确定地理限制操作的用户位置。", “原因”:“未知位置” }], "message" : "无法确定地理受限操作的用户位置。" }

关于此错误场景的信息不足。 任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: java json google-app-engine google-books


    【解决方案1】:

    嗯,这可能是因为 IP 无法用于定位用户。查看错误消息并在此处进行一些谷歌搜索是有道理的:

    https://productforums.google.com/forum/#!topic/books-api/88Ml3YIpvLw

    尝试将 &country=GB 添加到请求的末尾,或者任何 2 个字母代表您要搜索的国家/地区。 (更多:http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

    这个答案主要来自给定链接中的答案,但它似乎有效并且需要一些寻找。希望对你有帮助。

    【讨论】:

      【解决方案2】:

      以下代码使用 Google Books API 本身解决了我的问题。

      ///////////////////////////////////////////////////
      
      UrlFetchTransport url = new UrlFetchTransport();
      
      final Books books = new Books.Builder(
          url, jsonFactory, null)
          .setApplicationName(APPLICATION_NAME)
          .setGoogleClientRequestInitializer(
          new GBookRequest()).build();
      
      List volumesList = books.volumes().list("isbn:9780199562855");      
      
      // Execute the query.
      Volumes volumes = volumesList.execute();
          if (volumes.getTotalItems() == 0 || volumes.getItems() == null) {
          log.info("No matches found in GBooks.");
          return null;
      }
      
      ///////////////////////////////////////////////////
      
      public class GBookRequest extends BooksRequestInitializer {
      
          private static String apiKey = "xxxxxx";
      
          public GBookRequest() {
          super(apiKey);
          }
      
          @Override
          public void initializeBooksRequest(BooksRequest  request)
          throws IOException {
      
          request.set("country", "US");
          }
      }
      
      ///////////////////////////////////////////////////
      

      【讨论】:

        猜你喜欢
        • 2019-06-05
        • 2019-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-09-06
        • 2018-07-04
        • 2013-10-27
        • 2016-05-24
        • 2020-01-05
        相关资源
        最近更新 更多