尝试在 GAE 中使用 google api 客户端访问 google 地点

发布于 2024-11-08 18:40:40 字数 1387 浏览 4 评论 0原文

我正在尝试使用如下代码从 appengine 访问 google place api:

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2024-11-15 18:40:40

最后很简单,我混合了 get 和 post 动词:

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文