Question

I am trying to access google places api from appengine using code like this:

String PLACES_DETAILS_URL = "https://maps.googleapis.com/maps/api/place/details/json";

       // setup up the HTTP transport
    HttpTransport transport = new UrlFetchTransport();
    // add default headers
    GoogleHeaders defaultHeaders = new GoogleHeaders();
    transport.defaultHeaders = defaultHeaders;
   transport.defaultHeaders.put("Content-Type", "application/json");
    JsonHttpParser parser = new JsonHttpParser();
    parser.jsonFactory = new JacksonFactory();
    transport.addParser(parser);

    // build the HTTP GET request and URL
    HttpRequest request = transport.buildGetRequest();
    request.setUrl(PLACES_DETAILS_URL);
    GenericData data = new GenericData();
    data.put("reference", restaurantGoogleId);
    data.put("sensor", "false");
    data.put("key", ApplicationConstants.GoogleApiKey);
    JsonHttpContent content = new JsonHttpContent();
    content.jsonFactory=new JacksonFactory();
    content.data = data;
    request.content = content;

    try {
        HttpResponse response =  request.execute();
        String r = response.parseAsString();

        r=r;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I don't know even if this is the recommended way. If so, why this doesn't work?If I put a request in the browser directly it works, but with this code it always returns me "Request Denied".

Thanks in advance.

Was it helpful?

Solution

At the end it was easy, I mixed get and post verbs:

HttpTransport transport = new UrlFetchTransport();
        // add default headers
        GoogleHeaders defaultHeaders = new GoogleHeaders();
        transport.defaultHeaders = defaultHeaders;
       transport.defaultHeaders.put("Content-Type", "application/json");
       JsonCParser parser = new JsonCParser();
        parser.jsonFactory = new JacksonFactory();
        transport.addParser(parser);

        // build the HTTP GET request and URL
        HttpRequest request = transport.buildGetRequest();
        request.setUrl("https://maps.googleapis.com/maps/api/place/details/json?reference=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=<APIKEY>");


        try {
            HttpResponse response =  request.execute();
            String r = response.parseAsString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top