2011-10-13 110 views
2

我正在開發一個必須執行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 =)

我使用此代碼生成該中心提供全方位網址:

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; 
    } 
} 

的代碼工作正常:它返回一個網址,但該網址提供了這個錯誤:

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

我試圖改變客戶端ID(XXXXXXXXXXX.apps.googleusercontent.com)由XXXXXXXXXXX但它仍然無法正常工作。任何人都知道我在做什麼錯了?

非常感謝!

回答

0

問題是你沒有達到谷歌端點。即使沒有地方,api服務得到您的請求。

嘗試使用以下urlString:

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

的重要區別是googleapis而不是google。在瀏覽器中輸入你創建的url,你會看到,你得到了一些json(即使它是拒絕的請求)。然後你知道你已經達到了api-endpoint。

編輯:我認爲谷歌最近已將該域名更改爲googleapis。您使用的西班牙文文檔使用google,英文文檔使用googleapis。我認爲西班牙文檔不是最新的。也許你發佈信息穀歌(也許在論壇上)

+0

你是完全正確的,從現在起我將使用英文文檔,非常感謝你! – Juliet