| 1 | package Net::GoogleDrive; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use common::sense; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Mouse; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use HTTP::Request::Common; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Net::GoogleDrive - A Google Drive API interface | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 VERSION | ||||||||||||
| 15 | |||||||||||||
| 16 | Version 0.02 | ||||||||||||
| 17 | |||||||||||||
| 18 | =cut | ||||||||||||
| 19 | |||||||||||||
| 20 | 1 | our $VERSION = '0.02'; | |||||||||||
| 21 | |||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||
| 23 | |||||||||||||
| 24 | Google Drive API is basd on OAuth. I try to abstract as much away as | ||||||||||||
| 25 | possible so you should not need to know too much about it. Kudos to | ||||||||||||
| 26 | Net::Dropbox::API. | ||||||||||||
| 27 | |||||||||||||
| 28 | This is how it works: | ||||||||||||
| 29 | |||||||||||||
| 30 | use Net::GoogleDrive; | ||||||||||||
| 31 | |||||||||||||
| 32 | my $gdrive = Net::GoogleDrive->new(); | ||||||||||||
| 33 | my $login_link = $gdrive->login_link(); | ||||||||||||
| 34 | |||||||||||||
| 35 | ... Time passes and the login link is clicked ... | ||||||||||||
| 36 | |||||||||||||
| 37 | my $gdrive = Net::GoogleDrive->new(); | ||||||||||||
| 38 | |||||||||||||
| 39 | # $code will come from CGI or somesuch: Google gives it to you | ||||||||||||
| 40 | $gdrive->token($code); | ||||||||||||
| 41 | |||||||||||||
| 42 | my $files = $gdrive->files(); | ||||||||||||
| 43 | |||||||||||||
| 44 | foreach my $f (@{ $files->{items} }) { | ||||||||||||
| 45 | if ($f->{downloadUrl}) { | ||||||||||||
| 46 | open(my $fh, ">", "file.dl") or die("file.dl: $!\n"); | ||||||||||||
| 47 | print($fh $gdrive->downloadUrl($f)); | ||||||||||||
| 48 | close($fh); | ||||||||||||
| 49 | } | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | =head1 FUNCTIONS | ||||||||||||
| 53 | |||||||||||||
| 54 | =cut | ||||||||||||
| 55 | |||||||||||||
| 56 | 0 | ▶ | has 'ua' => (is => 'rw', isa => 'LWP::UserAgent', default => sub { LWP::UserAgent->new() }); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 57 | 1 | has 'debug' => (is => 'rw', isa => 'Bool', default => 0); | |||||||||||
| 58 | 1 | has 'error' => (is => 'rw', isa => 'Str', predicate => 'has_error'); | |||||||||||
| 59 | 1 | has 'scope' => (is => 'rw', isa => 'Str', required => 'Str'); | |||||||||||
| 60 | 1 | has 'redirect_uri' => (is => 'rw', isa => 'Str', required => 'Str'); | |||||||||||
| 61 | 1 | has 'client_id' => (is => 'rw', isa => 'Str', required => 'Str'); | |||||||||||
| 62 | 1 | has 'client_secret' => (is => 'rw', isa => 'Str'); | |||||||||||
| 63 | 1 | has 'access_token' => (is => 'rw', isa => 'Str'); | |||||||||||
| 64 | |||||||||||||
| 65 | =head2 login_link | ||||||||||||
| 66 | |||||||||||||
| 67 | This returns the login URL. This URL has to be clicked by the user and the user then has | ||||||||||||
| 68 | to accept the application in Google Drive. | ||||||||||||
| 69 | |||||||||||||
| 70 | Google Drive then redirects back to the callback URI defined with | ||||||||||||
| 71 | C<$self-E<gt>redirect_uri>. | ||||||||||||
| 72 | |||||||||||||
| 73 | =cut | ||||||||||||
| 74 | |||||||||||||
| 75 | sub login_link | ||||||||||||
| 76 | 0 | ▶ | { | ||||||||||
|
Subroutine
login_link: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||
| 78 | |||||||||||||
| 79 | 0 | my $uri = URI->new('https://accounts.google.com/o/oauth2/auth'); | |||||||||||
| 80 | |||||||||||||
| 81 | 0 | $uri->query_form ( | |||||||||||
| 82 | response_type => "code", | ||||||||||||
| 83 | client_id => $self->client_id(), | ||||||||||||
| 84 | redirect_uri => $self->redirect_uri(), | ||||||||||||
| 85 | scope => $self->scope(), | ||||||||||||
| 86 | ); | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | return($uri->as_string()); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | =head2 token | ||||||||||||
| 92 | |||||||||||||
| 93 | This returns the Google Drive access token. This is needed to | ||||||||||||
| 94 | authorize with the API. | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | sub token | ||||||||||||
| 99 | 0 | ▶ | { | ||||||||||
|
Subroutine
token: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||
| 101 | 0 | my $code = shift; | |||||||||||
| 102 | |||||||||||||
| 103 | 0 | ▶ | my $req = &HTTP::Request::Common::POST( | ||||||||||
|
Condition0%0/2
$self->client_secret || die('no client_secret given')
MC/DC0%0/1
$self->client_secret || die('no client_secret given')
$self->client_secret
Truth table0%0/2
A
A= $self->client_secret
| |||||||||||||
| 104 | 'https://accounts.google.com/o/oauth2/token', | ||||||||||||
| 105 | [ | ||||||||||||
| 106 | code => $code, | ||||||||||||
| 107 | client_id => $self->client_id(), | ||||||||||||
| 108 | client_secret => $self->client_secret() || die("no client_secret given"), | ||||||||||||
| 109 | redirect_uri => $self->redirect_uri(), | ||||||||||||
| 110 | grant_type => 'authorization_code', | ||||||||||||
| 111 | ] | ||||||||||||
| 112 | ); | ||||||||||||
| 113 | |||||||||||||
| 114 | 0 | my $ua = $self->ua(); | |||||||||||
| 115 | 0 | my $res = $ua->request($req); | |||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | if ($res->is_success()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | my $token = JSON::from_json($res->content()); | |||||||||||
| 119 | 0 | $self->access_token($token->{access_token}); | |||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | print "Got Access Token ", $res->access_token(), "\n" if $self->debug(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | } | ||||||||||||
| 123 | else { | ||||||||||||
| 124 | 0 | $self->error($res->status_line()); | |||||||||||
| 125 | 0 | warn "Something went wrong: ".$res->status_line(); | |||||||||||
| 126 | } | ||||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | =head2 files | ||||||||||||
| 130 | |||||||||||||
| 131 | This returns a files Resource object from JSON. | ||||||||||||
| 132 | |||||||||||||
| 133 | =cut | ||||||||||||
| 134 | |||||||||||||
| 135 | sub files | ||||||||||||
| 136 | 0 | ▶ | { | ||||||||||
|
Subroutine
files: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||
| 138 | |||||||||||||
| 139 | 0 | my $req = HTTP::Request->new( | |||||||||||
| 140 | GET => 'https://www.googleapis.com/drive/v2/files', | ||||||||||||
| 141 | HTTP::Headers->new(Authorization => "Bearer " . $self->access_token()) | ||||||||||||
| 142 | ); | ||||||||||||
| 143 | |||||||||||||
| 144 | 0 | my $res = $self->ua()->request($req); | |||||||||||
| 145 | |||||||||||||
| 146 | 0 | ▶ | if ($res->is_success()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | 0 | my $list = JSON::from_json($res->content()); | |||||||||||
| 148 | |||||||||||||
| 149 | 0 | return($list); | |||||||||||
| 150 | } | ||||||||||||
| 151 | else { | ||||||||||||
| 152 | 0 | $self->error($res->status_line()); | |||||||||||
| 153 | 0 | warn "Something went wrong: ".$res->status_line(); | |||||||||||
| 154 | 0 | return(undef); | |||||||||||
| 155 | } | ||||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | =head2 downloadUrl | ||||||||||||
| 159 | |||||||||||||
| 160 | This returns the binary data from a file. | ||||||||||||
| 161 | |||||||||||||
| 162 | =cut | ||||||||||||
| 163 | |||||||||||||
| 164 | sub downloadUrl | ||||||||||||
| 165 | 0 | ▶ | { | ||||||||||
|
Subroutine
downloadUrl: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||
| 167 | 0 | my $file = shift; | |||||||||||
| 168 | |||||||||||||
| 169 | my $req = HTTP::Request->new( | ||||||||||||
| 170 | GET => $$file{downloadUrl}, | ||||||||||||
| 171 | 0 | HTTP::Headers->new(Authorization => "Bearer " . $self->access_token()) | |||||||||||
| 172 | ); | ||||||||||||
| 173 | |||||||||||||
| 174 | 0 | my $res = $self->ua()->request($req); | |||||||||||
| 175 | |||||||||||||
| 176 | 0 | ▶ | if ($res->is_success()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 177 | 0 | return($res->content()); | |||||||||||
| 178 | } | ||||||||||||
| 179 | else { | ||||||||||||
| 180 | 0 | $self->error($res->status_line()); | |||||||||||
| 181 | 0 | warn "Something went wrong: ".$res->status_line(); | |||||||||||
| 182 | 0 | return(undef); | |||||||||||
| 183 | } | ||||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | =head2 FUTURE | ||||||||||||
| 187 | |||||||||||||
| 188 | More can be added if there is interest. | ||||||||||||
| 189 | |||||||||||||
| 190 | =cut | ||||||||||||
| 191 | |||||||||||||
| 192 | =head1 AUTHOR | ||||||||||||
| 193 | |||||||||||||
| 194 | Brian Medley, C<< <bpmedley at cpan.org> >> | ||||||||||||
| 195 | |||||||||||||
| 196 | =head1 BUGS | ||||||||||||
| 197 | |||||||||||||
| 198 | There are plenty. | ||||||||||||
| 199 | |||||||||||||
| 200 | =head1 SUPPORT | ||||||||||||
| 201 | |||||||||||||
| 202 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 203 | |||||||||||||
| 204 | perldoc Net::GoogleDrive | ||||||||||||
| 205 | |||||||||||||
| 206 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 207 | |||||||||||||
| 208 | Copyright 2012 Brian Medley. | ||||||||||||
| 209 | |||||||||||||
| 210 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 211 | under the terms of either: the GNU General Public License as published | ||||||||||||
| 212 | by the Free Software Foundation; or the Artistic License. | ||||||||||||
| 213 | |||||||||||||
| 214 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||
| 215 | |||||||||||||
| 216 | =cut | ||||||||||||
| 217 | |||||||||||||
| 218 | 1 | 1; | |||||||||||