Search code examples
javajsongoogle-app-enginegoogle-books

Accessing Books API through AppEngine fails with error code 400


I'm getting following error when accessing Google Books APIs from Google AppEngine Application.

API key for server application is used.

But if you run application locally in eclipse there is no problem.

{
  "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."
}

There is not enough information on this error scenario. Any help is appreciated. Thanks.


Solution

  • This following code solved my problem using Google Books API itself.

    ///////////////////////////////////////////////////
    
    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");
        }
    }
    
    ///////////////////////////////////////////////////