Google地方信息自动填充API访问被拒绝/需要API密钥

时间:2017-10-23 14:43:35

标签: java apache google-places-api

编辑(答案):KEY区分大小写。我使用Key代替key

我正在创建一个简单的HTTP请求谷歌放置自动完成API但我收到错误 https://developers.google.com/places/web-service/autocomplete

Code: 200
Response: OK
HTTP/1.1 200 OK
   "error_message" : "This service requires an API key.",
   "predictions" : [],
   "status" : "REQUEST_DENIED"

我的密钥已启用且实时与" Google Places API网络服务"相关。

这是我的示例代码。我做错了什么?

    import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class GoogleAPI {

    public static void main(String[] args) throws URISyntaxException, ClientProtocolException, IOException {
        CloseableHttpClient client = HttpClients.createDefault();

        URI uri = new URIBuilder().setScheme("https")
                .setHost("maps.googleapis.com")
                .setPath("maps/api/place/autocomplete/json")
                .setParameter("input", "London")
                .setParameter("Key", "*********")
                .build();

        HttpGet getMethod = new HttpGet(uri);

        HttpResponse response = client.execute(getMethod);

        System.out.println("Code: " +   response.getStatusLine().getStatusCode());
        System.out.println(("Response: ")+  response.getStatusLine().getReasonPhrase() );
        System.out.println(response.getStatusLine().toString());

        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);



    }

}

我藏了钥匙。

0 个答案:

没有答案