通过AppEngine访问Books API失败,错误代码为400

时间:2014-06-27 20:21:15

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

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

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

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

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Cannot determine user location for geographically restricted operation.",
    "reason" : "unknownLocation"
  } ],
  "message" : "Cannot determine user location for geographically restricted operation."
}

此错误情况的信息不足。 任何帮助表示赞赏。感谢。

2 个答案:

答案 0 :(得分:1)

以下代码使用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");
    }
}

///////////////////////////////////////////////////

答案 1 :(得分:0)

嗯,这可能是因为IP不能用于定位用户。通过这里查看错误消息并进行一些谷歌搜索是有道理的:

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

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

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