2011-10-13 4 views
2

Google Places API 요청을 수행해야하는 애플리케이션을 개발 중입니다.유효한 URL 생성 Google Places API

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

클라이언트 ID가 : XXXXXXXXXXX.apps.googleusercontent.com이

클라이언트의 비밀 : YYYYYYYYYYYYYYYYYY이 (가 보이는

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

나는 다음 웹 사이트에서 개인 키를 가지고 vNIXE0xscrmjlyV-12Nj_BvUPaw =)

이 코드를 사용하여 이 URL을 반환하지만 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을 생이

  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

문제는 Google 측에서 엔드 포인트에 도달하지 못했기 때문입니다. Api-service가 귀하의 요청을받은 곳이 없습니다. 중요한 차이가 googleapis 대신 google입니다

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

:

는 다음 URLString는을 사용하려고합니다. 브라우저에 작성한 URL을 입력하면 거부 된 요청 일지라도 json을 얻을 수 있습니다. 그런 다음 api 엔드 포인트에 도달했음을 알 수 있습니다.

편집 : 저는 최근 googleapis로 도메인을 변경했다고 생각합니다. 사용하는 스페인어 설명서는 google이고 영어 설명서는 googleapis입니다. 나는 스페인어 문서가 최신이 아닌 것 같아. 어쩌면 그 정보를 Google에 게시 할 수 있습니다 (아마도 포럼에)

+0

당신은 완전히 옳습니다, 지금부터는 영어로 된 문서를 사용할 것입니다, 정말 고마워요! – Juliet

관련 문제