| 1 | ########################################### | ||||||||||||||||||||||||||||
| 2 | package Net::Google::Drive::Simple; | ||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | use constant { | ||||||||||||||||||||||||||||
| 9 | 3 | 'DEFAULT_VERSION' => 2, | |||||||||||||||||||||||||||
| 10 | 'RECOMMENDED_VERSION' => 3, | ||||||||||||||||||||||||||||
| 11 | 3 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Net::Google::Drive::Simple::V2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Net::Google::Drive::Simple::V3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | our $VERSION = '3.01'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ########################################### | ||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | ########################################### | ||||||||||||||||||||||||||||
| 21 | 3 | ▶ | my ( $class, %options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | my $version = $options{'version'} || DEFAULT_VERSION(); | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'version'} || 2
MC/DC100%1/1
$options{'version'} || 2
$options{'version'}
Truth table100%2/2
A
A= $options{'version'}
| |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | if ( $version != DEFAULT_VERSION() && $version != RECOMMENDED_VERSION() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$version != 2 and $version != 3
MC/DC0%0/2
$version != 2 and $version != 3
$version != 2
$version != 3
Truth table66%2/3
A and B
A= $version != 2B= $version != 3
| |||||||||||||||||||||||||||||
| 25 | 0 | die "Incorrect version number ($version) - must be '2' or '3'"; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | my $impl = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | $version == DEFAULT_VERSION() | ||||||||||||||||||||||||||||
| 30 | ? Net::Google::Drive::Simple::V2->new(%options) | ||||||||||||||||||||||||||||
| 31 | : Net::Google::Drive::Simple::V3->new(%options); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | return $impl; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | 1; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | __END__ | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 NAME | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Net::Google::Drive::Simple - Simple modification of Google Drive data | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | use feature 'say'; | ||||||||||||||||||||||||||||
| 47 | use Net::Google::Drive::Simple; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # requires a ~/.google-drive.yml file with an access token, | ||||||||||||||||||||||||||||
| 50 | # see description below. | ||||||||||||||||||||||||||||
| 51 | my $gd = Net::Google::Drive::Simple->new( 'version' => 3 ); # v3 interface (RECOMMENDED!) | ||||||||||||||||||||||||||||
| 52 | my $gd = Net::Google::Drive::Simple->new(); # v2 interface (OUTDATE!) | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | my $children = $gd->children( "/" ); # or any other folder /path/location | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | foreach my $item ( @$children ) { | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # item is a Net::Google::Drive::Simple::Item object | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | if ( $item->is_folder ) { | ||||||||||||||||||||||||||||
| 61 | say "** ", $item->title, " is a folder"; | ||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | say $item->title, " is a file ", $item->mimeType; | ||||||||||||||||||||||||||||
| 64 | eval { # originalFilename not necessary available for all files | ||||||||||||||||||||||||||||
| 65 | say $item->originalFilename(), " can be downloaded at ", $item->downloadUrl(); | ||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Net::Google::Drive::Simple authenticates with a user's Google Drive and | ||||||||||||||||||||||||||||
| 73 | offers several convenience methods to list, retrieve, and modify the data | ||||||||||||||||||||||||||||
| 74 | stored in the 'cloud'. See C<eg/google-drive-upsync> as an example on how | ||||||||||||||||||||||||||||
| 75 | to keep a local directory in sync with a remote directory on Google Drive. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | All methods are documented based on the version you use: | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =over 4 | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item * V3 (recommended) | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Create default V3 API: | ||||||||||||||||||||||||||||
| 84 | my $gd = Net::Google:Drive::Simple->new( 'version' => 3 ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | The methods available are documented in | ||||||||||||||||||||||||||||
| 87 | L<Net::Google::Drive::Simple::V3>. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item * V2 (default, outdated) | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Create default V2 API: | ||||||||||||||||||||||||||||
| 92 | my $gd = Net::Google:Drive::Simple->new(); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # or: | ||||||||||||||||||||||||||||
| 95 | my $gd = Net::Google:Drive::Simple->new( 'version' => 2 ); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | The methods available are documented in | ||||||||||||||||||||||||||||
| 98 | L<Net::Google::Drive::Simple::V2>. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =back | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 GETTING STARTED | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | To get the access token required to access your Google Drive data via | ||||||||||||||||||||||||||||
| 105 | this module, you need to run the script C<eg/google-drive-init> in this | ||||||||||||||||||||||||||||
| 106 | distribution. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Before you run it, you need to register your 'app' with Google Drive | ||||||||||||||||||||||||||||
| 109 | and obtain a client_id and a client_secret from Google: | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | https://developers.google.com/drive/web/enable-sdk | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Click on "Enable the Drive API and SDK", and find "Create an API project in | ||||||||||||||||||||||||||||
| 114 | the Google APIs Console". On the API console, create a new project, click | ||||||||||||||||||||||||||||
| 115 | "Services", and enable "Drive API" (leave "drive SDK" off). Then, under | ||||||||||||||||||||||||||||
| 116 | "API Access" in the navigation bar, create a client ID, and make sure to | ||||||||||||||||||||||||||||
| 117 | register a an "installed application" (not a "web application"). "Redirect | ||||||||||||||||||||||||||||
| 118 | URIs" should contain "http://localhost". This will get you a "Client ID" | ||||||||||||||||||||||||||||
| 119 | and a "Client Secret". | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Then, replace the following lines in C<eg/google-drive-init> with the | ||||||||||||||||||||||||||||
| 122 | values received: | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # You need to obtain a client_id and a client_secret from | ||||||||||||||||||||||||||||
| 125 | # https://developers.google.com/drive to use this. | ||||||||||||||||||||||||||||
| 126 | my $client_id = "XXX"; | ||||||||||||||||||||||||||||
| 127 | my $client_secret = "YYY"; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Then run the script. It'll start a web server on port 8082 on your local | ||||||||||||||||||||||||||||
| 130 | machine. When you point your browser at http://localhost:8082, you'll see a | ||||||||||||||||||||||||||||
| 131 | link that will lead you to Google Drive's login page, where you authenticate | ||||||||||||||||||||||||||||
| 132 | and then allow the app (specified by client_id and client_secret above) access | ||||||||||||||||||||||||||||
| 133 | to your Google Drive data. The script will then receive an access token from | ||||||||||||||||||||||||||||
| 134 | Google Drive and store it in ~/.google-drive.yml from where other scripts can | ||||||||||||||||||||||||||||
| 135 | pick it up and work on the data stored on the user's Google Drive account. Make | ||||||||||||||||||||||||||||
| 136 | sure to limit access to ~/.google-drive.yml, because it contains the access | ||||||||||||||||||||||||||||
| 137 | token that allows everyone to manipulate your Google Drive data. It also | ||||||||||||||||||||||||||||
| 138 | contains a refresh token that this library uses to get a new access token | ||||||||||||||||||||||||||||
| 139 | transparently when the old one is about to expire. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 METHODS | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =over 4 | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item C<new()> | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Constructor, creates a helper object to retrieve Google Drive data | ||||||||||||||||||||||||||||
| 148 | later. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | While v2 (the outdated version) is still supported by Google, we | ||||||||||||||||||||||||||||
| 151 | recommend you use v3: | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Returns object of Net::Google::Drive::Simple::V3 | ||||||||||||||||||||||||||||
| 154 | my $gd = Net::Google::Drive::Simple->new( 'version' => 3 ); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Returns object of Net::Google::Drive::Simple::V2 | ||||||||||||||||||||||||||||
| 157 | my $gd = Net::Google::Drive::Simple->new( 'version' => 2 ); | ||||||||||||||||||||||||||||
| 158 | # or: | ||||||||||||||||||||||||||||
| 159 | my $gd = Net::Google::Drive::Simple->new(); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Read up on the methods in each class: L<Net::Google::Drive::Simple::V3> | ||||||||||||||||||||||||||||
| 162 | and L<Net::Google::Drive::Simple::V2>. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =back | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 Error handling | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | In case of an error while retrieving information from the Google Drive | ||||||||||||||||||||||||||||
| 169 | API, the methods above will return C<undef> and a more detailed error | ||||||||||||||||||||||||||||
| 170 | message can be obtained by calling the C<error()> method: | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | print "An error occurred: ", $gd->error(); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 LOGGING/DEBUGGING | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Net::Google::Drive::Simple is Log4perl-enabled. | ||||||||||||||||||||||||||||
| 177 | To find out what's going on under the hood, turn on Log4perl: | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 180 | Log::Log4perl->easy_init($DEBUG); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 LEGALESE | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Copyright 2012-2019 by Mike Schilli, all rights reserved. | ||||||||||||||||||||||||||||
| 185 | This program is free software, you can redistribute it and/or | ||||||||||||||||||||||||||||
| 186 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 2019, Nicolas R. <cpan@atoomic.org> | ||||||||||||||||||||||||||||
| 191 | 2012-2019, Mike Schilli <cpan@perlmeister.com> | ||||||||||||||||||||||||||||