生成有效的 URL Google Places API
我正在开发一个必须执行 Google Places API 请求的应用程序。
http://code.google.com/intl/es/apis/ maps/documentation/places/
我在以下网站上获取了私钥:
https://code.google.com/apis/console
客户端 ID:XXXXXXXXXXX.apps.googleusercontent.com
客户端密钥:YYYYYYYYYYYYYYYYYY(看起来像 vNIXE0xscrmjlyV-12Nj_BvUPaw= )
我正在使用此代码生成 URL:
public class UrlSigner {
// Note: Generally, you should store your private key someplace safe
// and read them into your code
private static String keyString = "YYYYYYYYYYYYYYYYYY";
// The URL shown in these examples must be already
// URL-encoded. In practice, you will likely have code
// which assembles your URL from user or web service input
// and plugs those values into its parameters.
private static String urlString = "http://maps.google.com/maps/api/place/search/json?location=40.717859,-73.957790&radius=1600&client=XXXXXXXXXXX.apps.googleusercontent.com&sensor=false";
// This variable stores the binary key, which is computed from the string (Base64) key
private static byte[] key;
public static void main(String[] args) throws IOException,
InvalidKeyException, NoSuchAlgorithmException, URISyntaxException {
// Convert the string to a URL so we can parse it
URL url = new URL(urlString);
UrlSigner signer = new UrlSigner(keyString);
String request = signer.signRequest(url.getPath(),url.getQuery());
System.out.println("Signed URL :" + url.getProtocol() + "://" + url.getHost() + request);
}
public UrlSigner(String keyString) throws IOException {
// Convert the key from 'web safe' base 64 to binary
keyString = keyString.replace('-', '+');
keyString = keyString.replace('_', '/');
System.out.println("Key: " + keyString);
this.key = Base64.decode(keyString);
}
public String signRequest(String path, String query) throws NoSuchAlgorithmException,
InvalidKeyException, UnsupportedEncodingException, URISyntaxException {
// Retrieve the proper URL components to sign
String resource = path + '?' + query;
// Get an HMAC-SHA1 signing key from the raw key bytes
SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");
// Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sha1Key);
// compute the binary signature for the request
byte[] sigBytes = mac.doFinal(resource.getBytes());
// base 64 encode the binary signature
String signature = Base64.encodeBytes(sigBytes);
// convert the signature to 'web safe' base 64
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');
return resource + "&signature=" + signature;
}
}
该代码工作正常:它返回一个 URL,但该 URL 给出以下错误:
- 这是一个错误。 在此服务器上找不到请求的 URL /maps/api/place/search/json?.(...)。这就是我们所知道的一切。
我尝试通过 XXXXXXXXXXX 更改 ClientID (XXXXXXXXXXX.apps.googleusercontent.com),但仍然无法正常工作。有人知道我做错了什么吗?
非常感谢!
I'm developing an application which has to do a Google Places API request.
http://code.google.com/intl/es/apis/maps/documentation/places/
I got the private key on the following website:
https://code.google.com/apis/console
Client ID: XXXXXXXXXXX.apps.googleusercontent.com
Client secret: YYYYYYYYYYYYYYYYYY (it looks like vNIXE0xscrmjlyV-12Nj_BvUPaw= )
I'm using this code to generate the URL:
public class UrlSigner {
// Note: Generally, you should store your private key someplace safe
// and read them into your code
private static String keyString = "YYYYYYYYYYYYYYYYYY";
// The URL shown in these examples must be already
// URL-encoded. In practice, you will likely have code
// which assembles your URL from user or web service input
// and plugs those values into its parameters.
private static String urlString = "http://maps.google.com/maps/api/place/search/json?location=40.717859,-73.957790&radius=1600&client=XXXXXXXXXXX.apps.googleusercontent.com&sensor=false";
// This variable stores the binary key, which is computed from the string (Base64) key
private static byte[] key;
public static void main(String[] args) throws IOException,
InvalidKeyException, NoSuchAlgorithmException, URISyntaxException {
// Convert the string to a URL so we can parse it
URL url = new URL(urlString);
UrlSigner signer = new UrlSigner(keyString);
String request = signer.signRequest(url.getPath(),url.getQuery());
System.out.println("Signed URL :" + url.getProtocol() + "://" + url.getHost() + request);
}
public UrlSigner(String keyString) throws IOException {
// Convert the key from 'web safe' base 64 to binary
keyString = keyString.replace('-', '+');
keyString = keyString.replace('_', '/');
System.out.println("Key: " + keyString);
this.key = Base64.decode(keyString);
}
public String signRequest(String path, String query) throws NoSuchAlgorithmException,
InvalidKeyException, UnsupportedEncodingException, URISyntaxException {
// Retrieve the proper URL components to sign
String resource = path + '?' + query;
// Get an HMAC-SHA1 signing key from the raw key bytes
SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");
// Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sha1Key);
// compute the binary signature for the request
byte[] sigBytes = mac.doFinal(resource.getBytes());
// base 64 encode the binary signature
String signature = Base64.encodeBytes(sigBytes);
// convert the signature to 'web safe' base 64
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');
return resource + "&signature=" + signature;
}
}
The code works fine: it returns a URL, but the URL gives this error:
- That’s an error.
The requested URL /maps/api/place/search/json?.(...) was not found on this server. That’s all we know.
I tried to change the ClientID (XXXXXXXXXXX.apps.googleusercontent.com) by XXXXXXXXXXX but it's still not working. Anyone know what I'm doing wrong?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您没有到达谷歌端的端点。甚至没有地方 API 服务收到您的请求。
尝试使用以下 urlString:
重要的区别是
googleapis
而不是google
。在浏览器中输入您创建的 url,然后您将看到,您会收到一些 json(即使它是被拒绝的请求)。然后您就知道您到达了 api 端点。编辑:我认为谷歌最近已将域名更改为googleapis。您使用的西班牙语文档使用
google
,英语文档使用googleapis
。我认为西班牙语文档不是最新的。也许您将该信息发布到谷歌(也许在论坛上)The problem is that you reached no endpoint on the google-side. Even no places-api-service got your request.
try to use the following urlString:
the important difference is
googleapis
instead ofgoogle
. Type your created url into your browser then you will see, that you get some json (even if it is a denied request). Then you know that your reached an api-endpoint.edit: I think google has changed the domain to googleapis recently. The spanish-documentation which you use uses
google
and the english-documentation usesgoogleapis
. I think the spanish documentation is not up-to-date. Maybe you post that info to google (maybe on the forum)