Skip to content

Commit

Permalink
Merge pull request #117 from RajeshGogo/driveV3-snippets
Browse files Browse the repository at this point in the history
test: Drive v3 snippets
  • Loading branch information
sqrrrl authored Jul 6, 2022
2 parents a83022e + 2d58e89 commit 04cab21
Show file tree
Hide file tree
Showing 20 changed files with 976 additions and 0 deletions.
43 changes: 43 additions & 0 deletions drive/snippets/drive_v3/src/DriveCreateDrive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_create_drive]
use Google\Client;
use Google\Service\Drive;
use Ramsey\Uuid\Uuid;
function createDrive()
{
try {
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);

$driveMetadata = new Drive\Drive(array(
'name' => 'Project Resources'));
$requestId = Uuid::uuid4()->toString();
$drive = $driveService->drives->create($requestId, $driveMetadata, array([
'fields' => 'id']));
printf("Drive ID: %s\n", $drive->id);
return $drive->id;
} catch(Exception $e) {
echo "Error Message: ".$e;
}

}
// [END drive_create_drive]
require_once 'vendor/autoload.php';
createDrive();
41 changes: 41 additions & 0 deletions drive/snippets/drive_v3/src/DriveCreateFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_create_folder]
use Google\Client;
use Google\Service\Drive;
function createFolder()
{
try {
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$fileMetadata = new Drive\DriveFile(array([
'name' => 'Invoices',
'mimeType' => 'application/vnd.google-apps.folder']));
$file = $driveService->files->create($fileMetadata, array([
'fields' => 'id']));
printf("Folder ID: %s\n", $file->id);
return $file->id;

}catch(Exception $e) {
echo "Error Message: ".$e;
}
}
// [END drive_create_folder]
require_once 'vendor/autoload.php';
createFolder();
44 changes: 44 additions & 0 deletions drive/snippets/drive_v3/src/DriveCreateShortcut.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_create_shortcut]
use Google\Client;
use Google\Service\Drive;
use Google\Service\Drive\DriveFile;
function createShortcut()
{
try {

$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$fileMetadata = new DriveFile(array([
'name' => 'Project plan',
'mimeType' => 'application/vnd.google-apps.drive-sdk']));
$file = $driveService->files->create($fileMetadata, array([
'fields' => 'id']));
printf("File ID: %s\n", $file->id);
return $file->id;

} catch(Exception $e) {
echo "Error Message: ".$e;
}

}
// [END drive_create_shortcut]
require_once 'vendor/autoload.php';
createShortcut();
45 changes: 45 additions & 0 deletions drive/snippets/drive_v3/src/DriveCreateTeamDrive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_create_team_drives]
use Google\Client;
use Google\Service\Drive;
use Google\Service\Drive\TeamDrive;
use Ramsey\Uuid\Uuid;
function createTeamDrive()
{
try {
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$teamDriveMetadata = new TeamDrive(array(
'name' => 'Project Resources'));
$requestId = Uuid::uuid4()->toString();
$teamDrive = $driveService->teamdrives->create($requestId, $teamDriveMetadata, array([
'fields' => 'id']));
printf("Team Drive ID: %s\n", $teamDrive->id);
return $teamDrive->id;

} catch(Exception $e) {

echo "Error Message: ".$e;
}

}
// [END drive_create_team_drives]
require_once 'vendor/autoload.php';
createTeamDrive();
43 changes: 43 additions & 0 deletions drive/snippets/drive_v3/src/DriveDownloadFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_download_file]
use Google\Client;
use Google\Service\Drive;
function downloadFile()
{
try {

$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$realFileId = readline("Enter File Id: ");
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$fileId = $realFileId;
$response = $driveService->files->get($fileId, array([
'alt' => 'media']));
$content = $response->getBody()->getContents();
return $content;

} catch(Exception $e) {
echo "Error Message: ".$e;
}

}
// [END drive_download_file]
require_once 'vendor/autoload.php';
downloadFile();
42 changes: 42 additions & 0 deletions drive/snippets/drive_v3/src/DriveExportPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_export_pdf]
use Google\Client;
use Google\Service\Drive;
function exportPdf()
{
try {
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$realFileId = readline("Enter File Id: ");
$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
$fileId = $realFileId;
$response = $driveService->files->export($fileId, 'application/pdf', array([
'alt' => 'media']));
$content = $response->getBody()->getContents();
return $content;

} catch(Exception $e) {
echo "Error Message: ".$e;
}

}
// [END drive_export_pdf]
require_once 'vendor/autoload.php';
exportPdf();
39 changes: 39 additions & 0 deletions drive/snippets/drive_v3/src/DriveFetchAppDataFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_fetch_appdata_folder]
use Google\Client;
use Google\Service\Drive;
function fetchAppDataFolder()
{
try {
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$client->addScope(Drive::DRIVE_APPDATA);
$driveService = new Drive($client);
$file = $driveService->files->get('appDataFolder', array([
'fields' => 'id'
]));
printf("Folder ID: %s\n", $file->id);
return $file->id;
} catch (Exception $e) {
echo "Error Message: ".$e;
}
}
// [END drive_fetch_appdata_folder]
require_once 'vendor/autoload.php';
fetchAppDataFolder();
54 changes: 54 additions & 0 deletions drive/snippets/drive_v3/src/DriveFetchChanges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright 2022 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.
*/
// [START drive_fetch_changes]
use Google\Client;
use Google\Service\Drive;
# TODO - PHP client currently chokes on fetching start page token
function fetchChanges()
{
try {
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
# Begin with our last saved start token for this user or the
# current token from getStartPageToken()
$savedStartPageToken = readLine("Enter Start Page Token: ");
$pageToken = $savedStartPageToken;
while ($pageToken != null) {
$response = $driveService->changes->listChanges($pageToken, array([
'spaces' => 'drive'
]));
foreach ($response->changes as $change) {
// Process change
printf("Change found for file: %s", $change->fileId);
}
if ($response->newStartPageToken != null) {
// Last page, save this token for the next polling interval
$savedStartPageToken = $response->newStartPageToken;
}
$pageToken = $response->nextPageToken;
}
echo $savedStartPageToken;
} catch(Exception $e) {
echo "Error Message: ".$e;
}

}
require_once 'vendor/autoload.php';
// [END drive_fetch_changes]
fetchChanges();
Loading

0 comments on commit 04cab21

Please sign in to comment.