开发者

Generate valid URL Google Places API

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, UnsupportedEncodingExcepti开发者_StackOverflowon, 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:

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


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:

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

the important difference is googleapis instead of google. 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 uses googleapis. I think the spanish documentation is not up-to-date. Maybe you post that info to google (maybe on the forum)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜