PHPのコマンドラインアプリでGoogle Driveに繋いでファイル一覧を取得する
自分用の備忘録の意味も込めてPHPで必要ソフトのインストールからファイル一覧の取得までを書きました。
ここでやるのは…
・サーバーのコマンドライン環境で認証ページへのリンクを発行
・発行したリンクに遷移し、Webブラウザでユーザー認証する。
・認証後に発行されたコードをコマンドラインに入力し、トークンを取得。
・トークンを使用して、認証したユーザがGoogle Driveにアップロードしたファイル一覧を表示する。
です。
環境
OS | CentOS7 |
PHP | 7.2.13 (※APIクライアントはPHPバージョン5.4以上で動作します。) |
API使用に最低必要なもの
・PHP実行環境
・API Client Library for PHP (Beta)
・資格情報ファイル(credentials.json)
APIライブラリでGoogle DriveのAPIを検索
Google APIsのページに移動してGoogle Drive APIを検索します。
https://console.developers.google.com/apis/library

Google Drive APIを押下し、有効化します。

有効化完了。

Composerのインストール
PHPクライアントはパッケージ管理ツールのComposerを使用します。
1 |
yum install composer |
続いてcomposer.jsonの作成です。
クライアントを使用する作業フォルダを作成。
1 2 |
mkdir gdrive cd gdrive/ |
作業フォルダへ移動したら 以下コマンドを実行。
今回は全部Enterで保存します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[root@localhost gdrive]# composer init Do not run Composer as root/super user! See https://getcomposer.org/root for details Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [root/gdrive]: Description []: Author [, n to skip]: n Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? Search for a package: Would you like to define your dev dependencies (require-dev) interactively [yes]? Search for a package: { "name": "root/gdrive", "require": {} } Do you confirm generation [yes]? |
composer.jsonに以下を追記して保存します。
1 2 3 |
"require": { "google/apiclient": "^2.0" } |
PHPクライアントのインストール
Google Driveに接続するためのクライアントをインストールします。
依存パッケージも同時に自動インストールされます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[root@localhost gdrive]# composer install Do not run Composer as root/super user! See https://getcomposer.org/root for details Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 13 installs, 0 updates, 0 removals - Installing ralouphie/getallheaders (2.0.5): Downloading (100%) - Installing psr/http-message (1.0.1): Downloading (100%) - Installing guzzlehttp/psr7 (1.5.2): Downloading (100%) - Installing guzzlehttp/promises (v1.3.1): Downloading (100%) - Installing guzzlehttp/guzzle (6.3.3): Downloading (100%) - Installing phpseclib/phpseclib (2.0.13): Downloading (100%) - Installing psr/log (1.1.0): Downloading (100%) - Installing monolog/monolog (1.24.0): Downloading (100%) - Installing firebase/php-jwt (v5.0.0): Downloading (100%) - Installing google/apiclient-services (v0.79): Downloading (100%) - Installing psr/cache (1.0.1): Downloading (100%) - Installing google/auth (v1.4.0): Downloading (100%) - Installing google/apiclient (v2.2.2): Downloading (100%) phpseclib/phpseclib suggests installing ext-libsodium (SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.) phpseclib/phpseclib suggests installing ext-mcrypt (Install the Mcrypt extension in order to speed up a few other cryptographic operations.) phpseclib/phpseclib suggests installing ext-gmp (Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.) monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB) monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server) monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required)) monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server) monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server) monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver) monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib) monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome) monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar) monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server) monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server) google/apiclient suggests installing cache/filesystem-adapter (For caching certs and tokens (using Google_Client::setCache)) Writing lock file Generating autoload files |
資格情報ファイル作成
APIの認証に使用するcredentials.jsonファイルを作成します。
以下のページに移動し「ENABLE THE DRIVE API」ボタンを押下。
https://developers.google.com/drive/api/v3/quickstart/php

プロジェクトを選択、もしくは新規作成したらNEXTを押します。
「DOWNLOAD CLIENT CONFIGURATION」を押して credentials.jsonをダウンロードし、クライアントの実行フォルダに配置します。

サンプルコードの配置
「Step 3: Set up the sample」にあるサンプルコードをquickstart.phpとして保存し、配置します。
ここまでで実行フォルダが以下のようになっていると思います。
1 2 3 4 5 |
./composer.json ./composer.lock ./credentials.json ./quickstart.php ./vendor |
いよいよ実行
quickstart.phpを実行します。
1 |
php quickstat.php |
実行すると、コマンドラインに認証ページへのリンクが表示されるのでリンク先へ移動し、認証します。

許可を押下すると、コードが表示されるので、そのコードをコマンドラインの
「Enter verification code:」の後に張り付けEnter。

すると、自分がGoogle Driveにアップロードしたファイル一覧が表示されます。
また、認証時にそのトークンを保存するtoken.jsonが作成されます。
有効期限が切れるまでは、毎回コードを求められません。
ディスカッション
コメント一覧
まだ、コメントがありません