On Thu, 31 Jul 2014 17:01:38 +0100 "mimic...@gmail.com" <mimic...@gmail.com> wrote:
> I receive error when calling GitHub API from Perl (LWP). Basically, > the problemis authentication failure, although the username and > password combination is valid. > > In the code snippet below, I have tried changing the values passed to > credentials() in several ways, however it does not work. [...] > my $auth_token = "username:token" [...] > my $ua = LWP::UserAgent->new; > $ua->agent('Mozilla/8.0'); > > #$ua->credentials("www.github.com:443","Authorization: token", > "$login", "$pass"); > $ua->credentials("api.github.com:443","Authorization > Basic:","$auth_token",""); The problem, I believe, is that LWP will send the details you've given it for that hostname and realm, when it sends a request and the remote server responds with a 401 Unauthorized response with a WWW-Authenticate header, stating the auth type and realm. Unfortunately, as you mentioned, GitHub does not do that; instead, it pretends the resource requested does not exist. So, LWP, never being asked to authenticate, will not do so. You'll need to manually do it by creating a HTTP::Request object rather than letting LWP do it for you. From some code of mine that successfully worked: my $url = "http://github.com/api/v2/json/pulls/" . $project; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => $url); # Auth if necessary if (my $auth = $self->auth_for_project($project)) { my ($user, $token) = split /:/, $auth, 2; $req->authorization_basic("$user/token", "$token"); } my $res = $ua->request($req) or return "Unknown - error fetching $url"; (this is from: https://github.com/bigpresh/Bot-BasicBot-Pluggable-Module-GitHub/blob/master/lib/Bot/BasicBot/Pluggable/Module/GitHub/PullRequests.pm#L67-L77 ) ... reading it, though, I'm not sure quite why it worked; GitHub's docs say that, if you're using a token, the username should be set as "$token:x-oauth-basic", and the password being unimportant; maybe this code worked against their older, now withdrawn v2 API. Anyway, I believe the lack of a proper WWW-Authenticate header from GitHub is what's causing your problems - they don't ask for auth, so LWP (correctly) doesn't send it. -- David Precious ("bigpresh") <dav...@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/