0

是否可以启用Google Placess API WebService以允许来自我的域的跨域请求,以便我可以直接从浏览器访问该服务?我一直在通过创建浏览器 API 密钥然后将我的域添加到引用列表来试验 API 密钥,但无济于事。不确定这是否是 referer 属性的用途。

这是设计上的限制,还是我在这里遗漏了什么?

Google Places API WebService 是我想要使用的服务。Places Library中的 Places Autocomplete 或 Places Search都不适合我的特殊要求。

干杯

斯蒂安

4

3 回答 3

0

让我说它不可能绕过。我尝试使用 java 而不是代码只适用于 http 请求(我在这里使用的是 graph.facebook.com):

public class search {
    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
        }
        return sb.toString();
      }

      public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
          String jsonText = readAll(rd);
          JSONObject json = new JSONObject(jsonText);
          return json;
        } finally {
          is.close();
        }
      }
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        System.getProperties().put("http.proxyHost", "172.16.0.2");
        System.getProperties().put("http.proxyPort", "8080");
        JSONObject json = readJsonFromUrl("http://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyBRlMLIdoTk-j4OZCucR47rVMLhMmvZVRw&type=hospital&location=12.8213125%2C80.0442&radius=500&_=1427359809583");
        System.out.println(json.toString());
       // System.out.println(json.get("about"));
       // System.out.println("hello ");
    }

}

如果您将链接替换为places api web search,它将无法正常工作,原因是谷歌没有在HTTP域上提供其服务,并且我的代码仅适用于HTTP域(不是HTTPS)

于 2015-03-27T08:46:43.543 回答
0

Places-API 也可以在Maps-Javascript-API中使用,您无需为那里的跨域而苦恼。

于 2012-03-05T20:48:15.547 回答
0

这是基于客户端的限制,所以简短的回答是:不。

然而,有些网站和服务试图通过使用脚本(动态加载它们)来解决这个问题。

看看这里这里(这些文章是关于通用跨域 AJAX 请求的)

于 2012-03-05T11:59:42.313 回答