damog / www-tumblr

Perl interface for the Tumblr API
https://metacpan.org/module/WWW::Tumblr
13 stars 8 forks source link

Need support for the new Tumblr API (OAuth-based) #2

Closed bucciarati closed 11 years ago

bucciarati commented 11 years ago

Tumblr's API v2 has superseded the old one (to the best of my understanding the old API is not working anymore, its docs are only there for e-rcheologists and sections about authentication and post submission have been completely removed).

It would be cool to have WWW::Tumblr support the new version (I can help with code and testing if needed).

Thanks!

netangel commented 11 years ago

Hi! Have you solved this issue? I was able to force Tumblr v2 API working with Net::OAuth module, I'm building the 'TumblrV2' library now to have all things combined. If you are ready to help – you are welcome, first version will be available today :)

damog commented 11 years ago

Great! I am ready to help. Wanna merge it on this repo?

netangel commented 11 years ago

I finished the base part of oAuth Tumblr v2 API, I've tested it only with blog posting, so it miss other methods for now, but it's easy to add them. Since I will not have much time to work on other methods implementation and testing till weekends, you can help here :). Any notes and advises are appreciated :)

Sources: https://github.com/netangel/www-tumblr/blob/master/lib/WWW/TumblrV2.pm

netangel commented 11 years ago

WWW::Tumblr is good module's name, I think we could do next steps:

  1. Finish and test new api implementation (TumblrV2)
  2. Replace old Tumblr.pm with new one
damog commented 11 years ago

Sounds good. I'll work on this tonight.

netangel commented 11 years ago

Sorry for lack of comments, I was tight in time :). I'm going to add some comments and documentation soon.

bucciarati commented 11 years ago

FWIW, I quickly tested it and it works for me:

% perl -w -Mstrict -Iwww-tumblr/lib -MWWW::TumblrV2 -E '
my $t = WWW::TumblrV2->new(
  blog =>         $ENV{blog},
  consumer_key => $ENV{consumer_key},
  secret_key =>   $ENV{secret_key},
  token =>        $ENV{token},
  token_secret => $ENV{token_secret},
);
say $t->post( type => "text", title => "eppur", body => "si muove" );
'
{"meta":{"status":201,"msg":"Created"},"response":{"id":55738049348}}

Awesome!

bucciarati commented 11 years ago

FYI I'm waiting on TumblrV2 to be released before I can release this: https://github.com/bucciarati/poe-component-irc-plugin-tumblr/commit/17fed1d84aa4642c22f89ccb4f3c0777aa35fd08

If one of you guys is working on V2 I'll just wait, otherwise just tell me and I can work on docs for it one of these evenings; I'd like to have it ready before the AmsterdamX.pm meeting on Monday 22 so I can spam it to people ;)

netangel commented 11 years ago

I'm planing to finish other methods in next 2 days. Also I want to create 2 examples, cause getting the auth url and token is quite tricky because of Net::OAuth features. Please note, I suppose the WWW::Tumblr should be more suitable name for module (TumblrV2 is the temporary development name :))

bucciarati commented 11 years ago

Will the example in my comment help? You can use that or tweak it: https://github.com/damog/www-tumblr/issues/2#issuecomment-21157290

Those arguments are all that is needed in order to submit posts. You may want to add some examples in the docs for other types of operations (following, reblogging, editing, deleting, ...).

Also you might want to link http://www.tumblr.com/docs/en/api/v2 and/or http://www.tumblr.com/oauth/apps which detail the steps needed to register an application and obtain OAuth tokens.

damog commented 11 years ago

@netangel: I faced pretty much the same problem with your TumblrV2 than with the stuff I didn't end up pushing: The access token and access token secret retrieval. I believe Net::OAuth has a problem on how Tumblr is handling that, for whatever reason. So my question is, how did you retrieve the token and token secret? Did you use the authorization_url method from Net::OAuth, etc? Because it doesn't work for me.

This is what I'm doing:

my $t = WWW::Tumblr->new(
    consumer_key => 'wakawaka',
    secret_key => 'wakawaka2'
);

print $t->authorization_url, "\n";
chomp( my $res = <STDIN> );

my ( $oauth_token, $oauth_verifier ) = $res =~ /oauth_token=(.+?)&oauth_verifier=(.+?)/;

my ( $token, $token_secret ) = $t->get_token( $oauth_token, $oauth_verifier );

print "I got token: $token \n";
print "I got token_secret: $token_secret \n";

print Dumper $t->post( type => "text", title => "eppur", body => "si muove" );

This is using TumblrV2. After the authorization_url gets printed, I go to it, allow access to it and Tumblr sends me to fake.com with query string parameters of oauth_token and oauth_verifier. Then I take those, paste, capture and pass them to get_token(). Invariably, I get a 403 from Net::OAuth with something like:

oauth_signature [EtqUniq3lWN1RXmrIo1eRGv8YBA=] does not match expected value [b3d4MZixYW+XW3F0xopuXs5x3SM=] at ../lib/WWW/Tumblr.pm line 73.

I briefly discussed with @bucciarati and he was getting those tokens from somewhere else. What's your case?

netangel commented 11 years ago

I've updated Tumblr.pm, a little. And added two examples. The Net::OAuth::Client is using session to store some temporary values, (the token => token_secret key-value, to be exact), this key-value first appears in Net::OAuth::Client::authorize_url, and then is used in Net::OAuth::Client::get_access_token but we cannot get if directly from authorize_url, so it needs to store session.
Please, check https://github.com/netangel/www-tumblr/blob/master/examples/tumblr.cgi, it will not compile, but you can get the idea. I'll make it fully working later.

When Tumblr.pm is used in 'live' environments like Dancer or Mojolicious, there is no need in store/restore session steps. Check the https://github.com/netangel/www-tumblr/blob/master/examples/mojo_tumblr, it's a small working example.

damog commented 11 years ago

Please checkout my origin/v2 branch. I have designed the base API to comply properly with Tumblr's. That is, using OAuth, API key or "none" authentication. There's three different buckets: User, Blog and, in the future, Tagged, as the documentation states. This is better as a WWW::Tumblr object can be used for different blogs or none at all but for the user.

use WWW::Tumblr;

my $t = WWW::Tumblr->new({
    consumer_key => 'foo',
    ... # the other 3
});

my $blog = $t->blog('myblog.tumblr.com'); #this will return a WWW::Tumblr::Blog object
if ( ! $blog->post({ type => 'text', title => 'Perl', body => 'rules!' }) ) {
    die "Failed! " . $blog->error->code;
} else { print "yeha!\n"; }

In this case, you can instantiate WWW::Tumblr::Blog directly with the same constructor arguments as WWW::Tumblr or just do it for WWW::Tumblr::User;

my $user = WWW::Tumblr::User>new({ the four tokens });
print Dumper $user->info; # etc....

I have implemented a bunch of OAuth authentication methods using @netangel's initial code. I will complete these soon and follow up on the API key and none ones that should be even simpler.

As said, this is all done on the v2 branch.

Cheers, dm.

netangel commented 11 years ago

Looks great. What methods are you implementing now? We can divide task to complete all Tumblr API methods. BTW, I cannot see the authorization_url and token receive methods in your version. How can I move your v2 branch to my master branch? (I gonna read the Git Book now) :)

damog commented 11 years ago

The authorization_url and get_token methods are still there but as unimplemented. They are under END. I was planning on bringing them back to life as soon as I could have most of the methods and base API ready. If you wanna pick that up, bring them to the WWW::Tumblr package, or even better a more suitable package and make it work with the Web app examples, that'd be sweet.

netangel commented 11 years ago

We can put the authorization_url and get_token methods in WWW::Tumblr or WWW::Tumblr::Tools, what do you think? Since there are no other methods connected with authorization/token we can implement them in the base class. Also I'll think how to do with sessions in better way.

damog commented 11 years ago

I would like to move all oauth methods into WWW::Tumblr::Authentication::OAuth. Same for APIKey and None. Tools could be there or even on WWW::Tumblr::Authentication::OAuth::Tools.

netangel commented 11 years ago

I've done with OAuth tools. But I've messed a little bit with merges, so just take a look at this files at my repo:

https://github.com/netangel/www-tumblr/blob/94af8cb4476070601b166f5bb52aedd8573178e1/lib/WWW/Tumblr.pm https://github.com/netangel/www-tumblr/blob/94af8cb4476070601b166f5bb52aedd8573178e1/lib/WWW/Tumblr/Authentication/OAuth.pm

And oauth examples: https://github.com/netangel/www-tumblr/tree/94af8cb4476070601b166f5bb52aedd8573178e1/examples

May be I'll create a more clean pull request later.

damog commented 11 years ago

Ah, that looks great. I'm currently working on adding test cases for all the methods so hopefully this can be released finally this week. If you can provide a pull request for this on the current master branch that'd be awesome. cheers.

netangel commented 11 years ago

Pull request created, please take a look. If you need any help with tests, please, let me know.
I'm planning to test the image upload procedure, cause I suppose it could be tricky.

damog commented 11 years ago

I have finally completed the basic full implementation of the API and uploaded it to PAUSE as WWW::Tumblr version 5.00_01. I plan to do some other minor fixes before uploading final 5.00.

@netangel: It'd be great if you could draft some documentation for the authorization work for oauth tools and etc.

Thanks everybody for the help!

damog commented 11 years ago

Here's the distribution:

https://metacpan.org/release/DAMOG/WWW-Tumblr-5.00_01