我在解决如何使用java从我的服务器以编程方式将同义词上传到google搜索API的问题。
1。授权:解释了如何将服务器设置为google api的说明here。我在哪里可以找到一个使用java的简单示例?
2。上传同义词:我创建了要上传的xml,解释为here。我无法看到我如何将其实际上传到google-api。有没有这样做的例子?
答案 0 :(得分: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", "my@gmail.com");
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);
}