NAME
Mojolicious::Plugin::OAuth - Plugin with OAuth support for Mojo and Mojolicious.
Uses Net::OAuth::All.
SYNOPSIS
#!/usr/bin/env perl
use Mojolicious::Lite;
use Data::Dumper;
my $config = {
provider1 => {
# provider1 config params
},
provider2 => {
# provider2 config params
},
};
# Mojolicious
$self->plugin('OAuth' => $config);
# ...
# Mojolicious::Lite
$ENV{'OAUTH_DEBUG'} = 1;
plugin 'OAuth' => $config;
get '/' => 'index';
get '/oauth_error' => 'oauth_error';
get '/login/:provider' => sub {
my $self = shift;
return $self->redirect_to('/oauth_error/') unless $self->helper('oauth_login' => $self->stash('provider'));
return;
};
get '/oauth/:provider' => sub {
my $self = shift;
return $self->redirect_to('/oauth_error/') unless $self->helper('oauth_callback');
my $oauth = $self->session('oauth');
my $res = $self->helper('oauth_request'
=> $oauth->{'oauth_provider'}
=> {
map {$_ => $oauth->{$_}} qw/access_token access_token_secret/
}
);
$self->render(
'data' => Dumper($res ? $res->json : {})
);
} => 'oauth';
get '/push' => sub {
my $self = shift;
my $oauth = $self->session('oauth');
my $res = $self->helper('oauth_request'
=> 'buzz'
=> 'POST'
=> 'https://www.googleapis.com/buzz/v1/activities/@me/@self'
=> {
map {$_ => $oauth->{$_}} qw/access_token access_token_secret/
}
=> {
alt => 'json',
}
=> {
'body' => q({"data": {"object": {"type": "note", "content": "I`m in Google BUZZ"}}}),
'headers' => {
'Content-Type' => 'application/json',
},
}
);
$self->render(
'data' => Dumper($res ? $res->json : {})
);
} => 'oauth';
app->start;
__DATA__
@@ index.html.ep
% layout 'funky';
<% for (keys %{$self->app->conf}) { %>
<a href="/login/<%= $_ %>/"><%= $_ %></a><br/>
<% } %>
@@ oauth.html.ep
% layout 'funky';
<%= $data %>
@@ oauth_error.html.ep
% layout 'funky';
Can`t login
@@ layouts/funky.html.ep
<!doctype html><html>
<head><title>OAuth TEST!</title></head>
<body><%== content %></body>
</html>
DESCRIPTION
Mojolicious::Plugin::OAuth is a plugin to easily enable OAuth authorization.
PLUGIN CONFIGURE plugin 'o_auth' => $config;
$config is required HASH reference. May consists of many provider configuration. For provider configuration see Net::OAuth::All.
HELPERS
All received data saves to session in 'oauth' param. Helper return undef on error.
- oauth_login($provider)
Make redirect_to to authorization url and obtain request_token and request_token_secret (only for OAuth less then 2.0) for provider $provider (is a key of $config).
$provider param saves to 'oauth_provider' of session('oauth') before execute.
- oauth_callback
Make oauth callback and obtain tokens for 'oauth_provider' saved on oauth_login step. All received data saves to session in 'oauth' param.
- oauth_request
You can make protected request to provider API
OAUTH REQUEST
Usage:
helper('oauth_request'
=> provider
=> request method
=> URL
=> core config
=> extra request params
=> custom request config
);
Description:
- provider
Is a key of $config.
- request method
HTTP Method for request.
- URL
Url to provider API.
- core config
Is a HASH reference with configuration params for temporary change of plugin core config. It`t may be access_token and access_token_secret(check examples).
- extra request params
Is a HASH reference with params of API (for example, 'status' for twitter).
- custom request config
Is a HASH reference. You can add or change headers ('headers' key, is a HASH reference) and put POST body - 'body' key, is a string (POST body generates from extra params by default). With 'body' param URL generates with extra params.
SEE ALSO
http://oauth.net
For more information about oauth provider configuration see Net::OAuth::All on http://github.com/likhatskiy/Net-OAuth-All
LICENSE AND COPYRIGHT
Copyright (C) 2011-2013 Anatoly Sharifulin Copyright (C) 2010 Alexey Likhatskiy
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.