Google-OAuth2-Client-Simple

 view release on metacpan or  search on metacpan

lib/Google/OAuth2/Client/Simple.pm  view on Meta::CPAN

package Google::OAuth2::Client::Simple;
# ABSTRACT: OAuth lib for Google OAuth 2.0
$Google::OAuth2::Client::Simple::VERSION = '0.004';
use Carp;
use Cpanel::JSON::XS;
use Furl;
use Moo;
use URI;

=head1 NAME

Google::OAuth2::Client::Simple - Client for Google OAuth2.

=head1 SYNOPSIS

    use Google::OAuth2::Client::Simple;

    my $google_client = Google::OAuth2::Client::Simple->new(
        client_id => $config->{client_id},
        client_secret => $config->{client_secret},
        redirect_uri => $config->{redirect_uri},
        scopes => ['https://www.googleapis.com/auth/drive.readonly', '...'],
    );

    within some page that connects to googleapis:
    if ( !$app->access_token() ) {
        $response = $google_client->request_user_consent();
        $response->content(); #show Googles html form to the user
    }

    then in your 'redirect_uri' route:
    my $token_ref = $google_client->exchange_code_for_token($self->param('code'), $self->param('state'));
    $app->access_token($token_ref->{access_token}); # set the access token somewhere (maybe in cache?), it lasts for an hour

=head1 DESCRIPTION

A client library that talks to Googles OAuth 2.0 API, found at:
https://developers.google.com/identity/protocols/OAuth2WebServer

Provides methods to cover the whole OAuth flow to get an access token and connect to the Google API.

To get credentials, register your app by following the instructions under "Creating web application credentials":
https://developers.google.com/identity/protocols/OAuth2WebServer

Valid scopes can be found here:
https://developers.google.com/identity/protocols/googlescopes

=head1 NOTE

It should be noted that token storage should be something handled by your application, if persistent usage is a requirement.
This client library doesn't do that because, well, it's simple ;)

=cut

has client_id => ( is => 'ro', required => 1 );
has client_secret => ( is => 'ro', required => 1 );
has redirect_uri => ( is => 'ro', required => 1 );

has login_hint => ( is => 'ro' );
has prompt => ( is => 'ro', default => sub { return 'consent' } );
has access_type => ( is => 'ro', default => sub { return 'offline' } );

has auth_uri => (
    is => 'ro',
    default => sub { return 'https://accounts.google.com/o/oauth2/v2/auth' }
);

has token_uri => (
    is => 'ro',
    default => sub { return 'https://www.googleapis.com/oauth2/v4/token' }
);

has revoke_uri => (
    is => 'ro',
    default => sub { return 'https://accounts.google.com/o/oauth2/revoke' }
);

has scopes => (
    is => 'rw',
    isa => sub {
        Carp::confess("Expecting scope to be an arrayref") unless ref($_[0]) eq 'ARRAY';
    },
    clearer => 1,
    required => 1,
);

has include_granted_scopes => (
    is => 'ro',
    isa => sub {
        Carp::confess("Only the strings 'true' or 'false' are valid options") unless $_[0] =~ m/^true|false$/;
    }
);

has state => (
    is => 'ro',
    lazy => 1,
    default => sub {
        return shift->client_id;
    },
);

has ua => (



( run in 0.571 second using v1.01-cache-2.11-cpan-39bf76dae61 )