google-api-php-client
Client ID と Client secret と Redirect URIs が必要です ( APIs Console )
前回、idtoken.php を使用してファイルのダウンロードを行いましたが、そもそも、どのファイルをダウンロードするかという事を決める為に、ファイルの一覧を取得する必要がありますが、Google のドキュメントを読むだけではなかなか解りにくいのです。なので、おそらくほとんどこれでまかなえると思われるオプションの指定方法を3種類ご紹介します。
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once "templates/base.php";
session_start();
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
require_once realpath(dirname(__FILE__) . '/../autoload.php');
$client_id = '';
$client_secret = '';
$redirect_uri = 'http://localhost/gapi/examples/idtoken.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setScopes('https://www.googleapis.com/auth/drive');
/************************************************
If we're logging out we just need to clear our
local access token in this case
************************************************/
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
/************************************************
If we have a code back from the OAuth 2.0 flow,
we need to exchange that with the authenticate()
function. We store the resultant access token
bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
/************************************************
If we have an access token, we can make
requests, else we generate an authentication URL.
************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
If we're signed in we can go ahead and retrieve
the ID token, which is part of the bundle of
data that is exchange in the authenticate step
- we only need to do a network call if we have
to retrieve the Google certificate to verify it,
and that can be cached.
************************************************/
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
// $token_data = $client->verifyIdToken()->getAttributes();
}
echo pageHeader("User Query - Retrieving An Id Token");
?>
<div class="box" style='width:100%'>
<div class="request">
<?php
// 接続用リンク
if (isset($authUrl)) {
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
}
// ログアウト用リンク
else {
echo "<a class='logout' href='?logout'>Logout</a>";
}
?>
</div>
<pre class="data">
<?php
if ( isset($_SESSION['access_token']) ) {
print "セッションの内容\n";
print_r($_SESSION);
// 内容が JSON なのでデコード
$obj = json_decode($_SESSION['access_token']);
print "JSON の内容\n";
print_r($obj);
print "アクセストークン\n";
print "{$obj->{'access_token'}}\n";
$dr_service = new Google_Service_Drive($client);
// 有効な画像ファイルを 10 エントリを取得する
$dr_results = $dr_service->files->listFiles(array('q' => 'mimeType contains "image/" and trashed != true and hidden != true', 'maxResults' => 10 ));
// $dr_results = $dr_service->files->listFiles(array('q' => '"0B9Jymqpro6gSb2w3bHZXU2dYUmM" in parents'));
// $dr_results = $dr_service->files->listFiles(array('q' => 'mimeType = "application/vnd.google-apps.folder"'));
// ファイルのインスタンスの配列を取得
$files = $dr_results->getItems();
print_r($files);
}
?>
</pre>
</div>
Search for Files
コメント行も含めて3つあります。maxResults の条件は特殊ですが、'q' => 条件 の指定に関しては、条件部分には and が使えていろいろ組み合す事ができます。
mimeType contains "image/" は、画像ファイルを検索します。
trashed != true は削除されていないファイルです。
( これを指定しないと、30日間は削除されても残っているようなので必須です )
hidden != true これは、システム側のファイルの属性のようなのですが、ドキュメントには無かったですが、指定したら動作しました。
"0B9Jymqpro6gSb2w3bHZXU2dYUmM" in parents は、フォルダID(この場合は、0B9Jymqpro6gSb2w3bHZXU2dYUmM) で示されるフォルダ内の一覧になります。フォルダID は、Google ドライブの管理画面の URL から直接取り出す事もできます。
mimeType = "application/vnd.google-apps.folder" は、単純にフォルダの一覧になるようです。
まだまだ、細かく検証はしていませんが、これがあると無いとではかなりの違いがあると思います。
関連する記事