I had a lot of trouble since Goodreads was randomly failing my authentication attempts; first I thought it was timing out the access key really fast, or that I was sending invalid requests, but then I determined (through much experimentation, and showing that the same request can both fail and succeed) that it just randomly fails (401 Invalid OAuth Request, without a WWW-Authenticate header). When that happens, you just need to keep trying.my $oalc = OAuth::Lite::Consumer->new( consumer_key => YOUR KEY HERE, consumer_secret => YOUR SECRET HERE, site => 'http://www.goodreads.com', request_token_path => '/oauth/request_token', access_token_path => '/oauth/access_token', authorize_path => '/oauth/authorize', );
The user has to go to that URL and approve your application (if it's a web application, you can redirect them there and pass a callback URL for it to send the user to afterward). When it's approved, get an access token:my $reqt = $oalc->get_request_token; my $url = $oalc->url_to_authorize(token => $reqt->token);
This token (available as $oalc->access_token) can be persisted (e.g. to a file or database: save the token and secret data) and then next time used to create a token to use to authenticate:$oalc->get_access_token;
Now you can make requests, using code like this, which returns a hash of information (id, link, name) about the current user:my $acct = OAuth::Lite::Token->new(token => $token, secret => $secret); $oalc->access_token($acct);
Even after the first successful request, subsequent requests can randomly fail. I have no idea why this is happening; I've asked Goodreads in the developer forum and elsewhere, but they aren't saying anything, although they were very helpful with some other requests, e.g. documenting some undocumented functionality for adding a read date to a review.sub get_user { my $res = $oalc->request(url => 'http://www.goodreads.com/api/auth_user'); unless ($res->is_success) { print "Failed to get user: ".$res->status_line."\n".$res->content."\n"; return; } my $u = $xs->parse_string($res->content)->{user}; # id, link, name return $u; }