javaを使用してAPI( http://zootool.com/api/docs/add )を介してzootoolページにページを追加しようとしています。このために、ダイジェスト認証を使用する必要があります。
ページの取得を承認するためのphpの例を示します。
<?php
$username = 'username';
$password = 'password';
$api_url = 'http://zootool.com/api/users/items/?username='
. $username . '&apikey=###';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
// HTTP Digest Authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, strtolower($username) . ':' . sha1($password));
curl_setopt($ch, CURLOPT_USERAGENT, 'My PHP Script');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$data = json_decode($result, true);
print_r($data);
?>
しかし、これはJavaでページを追加するためにどのように行われますか?検索したところ、apache commons HttpClientが役立つ可能性があることがわかりましたが、頭を包み込むことができないようです。
私は試した:
String url = "http://zootool.com/api/add/?url=http://www.google.com
&title=Google&apikey=###";
DigestScheme authscheme = new DigestScheme();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse authResponse = httpclient.execute(httpget);
Header challenge = authResponse.getHeaders("WWW-Authenticate")[0];
authscheme.processChallenge(challenge);
Header solution = authscheme.authenticate(
new UsernamePasswordCredentials("username", "password"),
new BasicHttpRequest(HttpPost.METHOD_NAME,
new URL(url).getPath()));
HttpResponse goodResponse = httpclient.execute(httpget);
(api-manualに記載されているように)パスワードをsh1-hashしようとしましたが、うまくいきませんでした。私のコードは、対応すべき課題を見つけることができないようです。
APIキー、ユーザー名、パスワードは正しいです。
更新プリエンプティブダイジェスト認証を使用する必要があることは比較的確信していますが、apacheによる回避策を試してみると、401と「認証エラー:ダイジェスト認証のチャレンジが必要ですが見つかりません」-Javaからの警告が表示されます。私が使用するコードは次のとおりです。
HttpHost targetHost = new HttpHost("www.zootool.com", 80, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("username", "sha1-password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local
// auth cache
DigestScheme digestAuth = new DigestScheme();
// Suppose we already know the realm name
digestAuth.overrideParamter("realm", "www.zootool.com");
// Suppose we already know the expected nonce value
digestAuth.overrideParamter("nonce", "something");
authCache.put(targetHost, digestAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
HttpGet httpget = new HttpGet("/api/add/?url=http://www.google.com&title=Google&apikey=###");
System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("to target: " + targetHost);
for (int i = 0; i < 3; i++) {
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
}
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
DigestSchemeパラメーターに意味のある値を指定する必要がありますか?私も正しい方向に進んでいますか?
/アンドレ