Question

I'm having issues figuring out how to programmatically upload synonyms to the google search api from my server using java.

1. The Authorization: The description of how to do a server to google api is explained here. Where can I find a simple example of this using java?

2. Upload synonyms: I have created the xml to be uploaded, explained here. I am not able to see how I actually upload this to the google-api. Is there an example of how this is done?

Was it helpful?

Solution

1. The Authorization

    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", "my@gmail.com");
      method.addParameter("Passwd", "myPassword");
      method.addParameter("service", "cprose");
      method.addParameter("source", "mySource");
      String response = executeMethodAsString(method);
      return retrieveAuthFromResponse(response);
    }

2. Upload synonym

    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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top