2012-05-09 4 views
1

java를 사용하여 내 서버에서 Google 검색 API에 동의어를 프로그래밍 방식으로 업로드하는 방법을 알아내는 데 문제가 있습니다.프로그래밍 방식으로 자바를 사용하는 Google 검색 API에 동의어 업로드

1. 승인 : 서버를 Google api로 보내는 방법의 설명은 here입니다. 어디에서 java를 사용하여 간단한 예제를 찾을 수 있습니까?

업로드 동의어 : here으로 업로드 할 XML을 만들었습니다. 실제로 google-api에 업로드하는 방법을 볼 수 없습니다. 이것이 어떻게 수행되는지에 대한 예가 있습니까?

답변

0

1. 권한 부여

public static String getAuthorizationToken() throws IOException, HttpException{ 
     PostMethod method = new PostMethod("https://www.google.com/accounts/ClientLogin"); 
     method.addParameter("accountType", "HOSTED_OR_GOOGLE"); 
     method.addParameter("Email", "[email protected]"); 
     method.addParameter("Passwd", "myPassword"); 
     method.addParameter("service", "cprose"); 
     method.addParameter("source", "mySource"); 
     String response = executeMethodAsString(method); 
     return retrieveAuthFromResponse(response); 
    } 

2. 업로드 동의어

public static String updateSynonyms(String authToken, String xml) throws HttpException, IOException{ 

     PostMethod method = new PostMethod("http://www.google.com/cse/api/default/synonyms/abcdefg1234"); 
     method.addRequestHeader("Content-Type", "text/xml"); 
     method.addRequestHeader("Authorization", "GoogleLogin auth=" + authToken); 
     RequestEntity entitiy = new StringRequestEntity(xml, "text/xml", "utf-8"); 
     method.setRequestEntity(entitiy); 
     return executeMethodAsString(method); 
    }