gpt4 book ai didi

java - 生成有效的 URL Google Places API

转载 作者:行者123 更新时间:2023-11-29 09:18:54 25 4
gpt4 key购买 nike

我正在开发一个必须执行 Google Places API 请求的应用程序。

http://code.google.com/intl/es/apis/maps/documentation/places/

我在以下网站上获得了私钥:

https://code.google.com/apis/console

客户端 ID:XXXXXXXXXXX.apps.googleusercontent.com

客户端密码:YYYYYYYYYYYYYYYYY(看起来像 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 给出了这个错误:

  1. That’s an error. The requested URL /maps/api/place/search/json?.(...) was not found on this server. That’s all we know.

我尝试通过 XXXXXXXXXXX 更改 ClientID (XXXXXXXXXXX.apps.googleusercontent.com),但它仍然无法正常工作。有人知道我做错了什么吗?

非常感谢!

最佳答案

问题是您没有到达 google 端的端点。甚至没有 places-api-service 收到您的请求。

尝试使用以下 urlString:

private static String urlString = "http://maps.googleapis.com/maps/api/place/search/json?location=..."

重要的区别是 googleapis 而不是 google。在浏览器中输入你创建的 url,然后你会看到,你会得到一些 json(即使它是一个被拒绝的请求)。然后您知道您到达了一个 api 端点。

编辑:我认为谷歌最近将域更改为 googleapis。您使用的西类牙语文档使用 google,而英语文档使用 googleapis。我认为西类牙语文档不是最新的。也许您将该信息发布到谷歌(也许在论坛上)

关于java - 生成有效的 URL Google Places API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7755976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com