WWW-Google-Drive

 view release on metacpan or  search on metacpan

lib/WWW/Google/Drive.pm  view on Meta::CPAN

# =========================================== _get_fields ========================================= #

sub _get_fields
{
    my $self = shift;

    if ($self->{file_fields_changed}) {
        $self->{file_fields_str} = join(',', @{$self->{file_fields}});
        $self->{file_fields_changed} = 0;
    }

    return $self->{file_fields_str};
}

# =========================================== OAuth ========================================= #

# =================================== _authorization_headers ================================ #

sub _authorization_headers
{
    my ($self) = @_;

    return ('Authorization' => 'Bearer ' . $self->_get_oauth_token);
}

# ====================================== _get_oauth_token =================================== #

sub _get_oauth_token
{
    my ($self) = @_;

    if (not exists $self->{oauth}) {
        $self->_authenticate or LOGDIE "Google drive authentication failed";
        return $self->{oauth}->{_access_token};
    }

    my $time_remaining = $self->{oauth}->{_expires} - time();

    # checking if the token is still valid for more than 5 minutes
    # why 5 minutes? simply :-).
    if ($time_remaining < 300) {
        $self->_authenticate() or LOGDIE "Google drive token refresh failed";
    }

    return $self->{oauth}->{_access_token};
}

# ========================================== _authenticate =================================== #

sub _authenticate
{
    my ($self) = @_;

    LOGDIE "Config JSON file " . $self->secret_json . " not exist!" unless (-f $self->secret_json);

    my $config = Config::JSON->new($self->secret_json);

    my $time = time;

    my $service_acc_id     = $config->get("client_email");
    my $private_key_string = $config->get("private_key");

    my $jwt = JSON::WebToken->encode(
        {
            iss   => $service_acc_id,
            scope => $self->scope,
            aud   => $self->token_uri,
            exp   => $time + 3600,
            iat   => $time,

            # Access files from this users drive/ impersonate user
            prn => $self->user_as,
        },
        $private_key_string,
        'RS256',
        {typ => 'JWT'}
    );

    # Authenticate via post, and get a token
    my $ua       = $self->user_agent;
    my $response = $ua->post(
        $self->token_uri,
        {
            grant_type => encode_entities('urn:ietf:params:oauth:grant-type:jwt-bearer'),
            assertion  => $jwt
        }
    );

    unless ($response->is_success()) {
        LOGDIE $response->code, $response->content;
    }

    my $data = decode_json($response->content);

    if(!$data->{access_token}){
        LOGDIE "Authentication failed with error code: ", $response->code, $response->content;
        return 0;
    }

    $self->{oauth}->{_access_token} = $data->{access_token};

    # expires_in is number of seconds the token is valid, storing the validity epoch
    $self->{oauth}->{_expires} = $data->{expires_in} + time;
    return 1;
}

# ========================================== _token_expire ==================================== #

sub _token_expire
{
    my ($self) = @_;
    $self->{oauth}->{_expires} = time - 1;
}

1;

__END__

=back

=head1 Error handling
 
In case of an error while retrieving information from the Google Drive
API, the methods above will return C<undef> and a more detailed error
message can be obtained by calling the C<error()> method:
  
    print "An error occurred: ", $gd->error();
       
=head1 LOGGING/DEBUGGING
 
WWW::Google::Drive is Log4perl-enabled.
To find out what's going on under the hood, turn on Log4perl:
  
    use Log::Log4perl qw(:easy);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.395 second using v1.00-cache-2.02-grep-82fe00e-cpan-f73e49a70403 )