Subscribed unsubscribe Subscribe Subscribe

tumblrのoauthのメモ

メモ。Net::OAuthじゃなくてOAuth::Lite使ったらすぐに出来た。時間あったらなんではまってたか調べたい。
そもそもoauth久しぶりに見た

use strict;
use warnings;

use OAuth::Lite::Consumer;
use Plack::Response;
use Plack::Request;
use Plack::Builder;

my $consumer = OAuth::Lite::Consumer->new(
    consumer_key => $consumer_key,
    consumer_secret => $consumer_secret,
    site => "http://www.tumblr.com",
    request_token_path => "/oauth/request_token",
    authorize_path => "/oauth/authorize",
    access_token_path => "/oauth/access_token",
);

my $app = sub {
    my $env = shift;

    my $req = Plack::Request->new($env);
    my $oauth_verifier = $req->query_parameters->{oauth_verifier};
    my $res = Plack::Response->new;
    my $session = $env->{'psgix.session'};

    if ( $oauth_verifier ) {
        my $request_token = $session->{request_token};
        my $access_token = $consumer->get_access_token(
            token => $request_token,
            verifier => $oauth_verifier,
        );

        $res->status(200);
        $res->content_type("text/plain");
        $res->body(sprintf("access_token:%s\naccess_token_secret:%s\n", $access_token->token, $access_token->secret))
    }
    else {
        my $request_token = $consumer->get_request_token;
        $session->{request_token} = $request_token;
        $res->redirect($consumer->url_to_authorize(
                token => $request_token,
            ));
    }

    $res->finalize;
};

builder {
    enable 'Session';
    $app;
};