Java zootool api授权
我正试图通过java的api向我的zootool页面添加一个页面。为此,我需要使用摘要身份验证 下面给出了获取页面授权的php示例:Java zootool api授权,java,authorization,digest,digest-authentication,Java,Authorization,Digest,Digest Authentication,我正试图通过java的api向我的zootool页面添加一个页面。为此,我需要使用摘要身份验证 下面给出了获取页面授权的php示例: <?php $username = 'username'; $password = 'password'; $api_url = 'http://zootool.com/api/users/items/?username=' . $username . '&apikey=###'; $ch = curl_
<?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);
?>
我尝试过按照api手册中的说明对密码进行sh1哈希,但没有成功。我的代码似乎找不到任何需要响应的挑战
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参数提供有意义的值?
我走对了吗
/安德烈先发制人的授权码有效!错误是必须将login=true添加到api url,如下所示:
HttpGet httpget = new HttpGet("/api/add/?url=http://www.google.com&title=Google&apikey=####&login=true");
api手册中没有此信息。抢占式授权代码有效!错误是必须将login=true添加到api url,如下所示:
HttpGet httpget = new HttpGet("/api/add/?url=http://www.google.com&title=Google&apikey=####&login=true");
api手册中没有此信息