Subscribed unsubscribe Subscribe Subscribe

oauth::lite2::client::webserver簡単で嬉しい

なーと思いました。まぁもともとそんな複雑な仕組みじゃないけどfoursquareのaccess_tokenをひっぱってくるとき
こういうのを書いた。

use strict;
use warnings;

use OAuth::Lite2::Client::WebServer;
use Plack::Request;

my $client = OAuth::Lite2::Client::WebServer->new(
    id                => "client_id",
    secret            => "client_secret",
    authorize_uri     => "https://ja.foursquare.com/oauth2/authorize",
    access_token_uri  => "https://ja.foursquare.com/oauth2/access_token",
);
my $redirect_uri = "redirect_uri";

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

    my $req = Plack::Request->new($env);
    my $code = $req->query_parameters->{code};

    if ( $code ) {
        my $access_toekn = $client->get_access_token(
            code  => $code,
            redirect_uri  => $redirect_uri,
        );

        my $res = $req->new_response(200);
        $res->content_type('text/plain');
        $res->body($access_toekn->access_token);
        return $res->finalize;
    }
    else {
        my $redirect_uri = $client->uri_to_redirect( redirect_uri => $redirect_uri );
        $res->redirect($redirect_uri);
    }

};


redirect_urihttp://localhost:5000とかで登録しといてplackupする