Java
Using Java's own client
uses a static call, independent of the client itself
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("USER","PASS".toCharArray());
}
};
Authenticator.setDefault(auth);Using Apache HttpClient
is applied to an instance of HttpClient
httpClient.getState().setCredentials( new UsernamePasswordCredentials(user, password) );
One benefit of using HttpClient is that it supports pre-emptive authentication which means that your credentials are automatically included in any request. When using the default Java Authenticator code, the JVM always makes an initial attempt to access remote resources without the credentials and then, after it fails, it'll provide them. This means that there's always one extra redundant HTTP request.
PHP
Using mod_curl
$request = curl_init("http://api.talis.com/stores/mystore/items");
curl_setopt($request, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($request, CURLOPT_USERPWD, "username:password");
$response = curl_exec($request);Python
You'll need something like this:
import urllib2
def postWithDigestAuth(self,data):
authhandler = urllib2.HTTPDigestAuthHandler()
authhandler.add_password(self.talis_realm, self.talis_uri, self.talis_user, self.talis_password)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
headers = {"User-Agent":"danja-todo-thing", "Content-Type":"application/rdf+xml"}
request = urllib2.Request(self.talis_uri, data, headers)
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, arg:
return arg
Shell
Using cURL
curl --digest -u"user:password" http://api.talis.com/stores/mystore/items
JavaScript
Using Jaxer (server side)
For example accessing OAI service which requires authentication.
var url = "http://api.talis.com/stores/{store}/services/oai-pmh?verb=ListRecords&metadataPrefix=oai_dc";
var options = new Jaxer.XHR.SendOptions();
options.username = "user";
options.password = "pass";
var content = Jaxer.Web.get(url, options);
Perl
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->credentials( "api.talis.com:80", "bigfoot", "user" => "password" );
my $response = $ua->get($uri);