Skip to content

Commit 04cab21

Browse files
authored
Merge pull request googleworkspace#117 from RajeshGogo/driveV3-snippets
test: Drive v3 snippets
2 parents a83022e + 2d58e89 commit 04cab21

20 files changed

+976
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_create_drive]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
use Ramsey\Uuid\Uuid;
21+
function createDrive()
22+
{
23+
try {
24+
$client = new Client();
25+
$client->useApplicationDefaultCredentials();
26+
$client->addScope(Drive::DRIVE);
27+
$driveService = new Drive($client);
28+
29+
$driveMetadata = new Drive\Drive(array(
30+
'name' => 'Project Resources'));
31+
$requestId = Uuid::uuid4()->toString();
32+
$drive = $driveService->drives->create($requestId, $driveMetadata, array([
33+
'fields' => 'id']));
34+
printf("Drive ID: %s\n", $drive->id);
35+
return $drive->id;
36+
} catch(Exception $e) {
37+
echo "Error Message: ".$e;
38+
}
39+
40+
}
41+
// [END drive_create_drive]
42+
require_once 'vendor/autoload.php';
43+
createDrive();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_create_folder]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
function createFolder()
21+
{
22+
try {
23+
$client = new Client();
24+
$client->useApplicationDefaultCredentials();
25+
$client->addScope(Drive::DRIVE);
26+
$driveService = new Drive($client);
27+
$fileMetadata = new Drive\DriveFile(array([
28+
'name' => 'Invoices',
29+
'mimeType' => 'application/vnd.google-apps.folder']));
30+
$file = $driveService->files->create($fileMetadata, array([
31+
'fields' => 'id']));
32+
printf("Folder ID: %s\n", $file->id);
33+
return $file->id;
34+
35+
}catch(Exception $e) {
36+
echo "Error Message: ".$e;
37+
}
38+
}
39+
// [END drive_create_folder]
40+
require_once 'vendor/autoload.php';
41+
createFolder();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_create_shortcut]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
use Google\Service\Drive\DriveFile;
21+
function createShortcut()
22+
{
23+
try {
24+
25+
$client = new Client();
26+
$client->useApplicationDefaultCredentials();
27+
$client->addScope(Drive::DRIVE);
28+
$driveService = new Drive($client);
29+
$fileMetadata = new DriveFile(array([
30+
'name' => 'Project plan',
31+
'mimeType' => 'application/vnd.google-apps.drive-sdk']));
32+
$file = $driveService->files->create($fileMetadata, array([
33+
'fields' => 'id']));
34+
printf("File ID: %s\n", $file->id);
35+
return $file->id;
36+
37+
} catch(Exception $e) {
38+
echo "Error Message: ".$e;
39+
}
40+
41+
}
42+
// [END drive_create_shortcut]
43+
require_once 'vendor/autoload.php';
44+
createShortcut();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_create_team_drives]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
use Google\Service\Drive\TeamDrive;
21+
use Ramsey\Uuid\Uuid;
22+
function createTeamDrive()
23+
{
24+
try {
25+
$client = new Client();
26+
$client->useApplicationDefaultCredentials();
27+
$client->addScope(Drive::DRIVE);
28+
$driveService = new Drive($client);
29+
$teamDriveMetadata = new TeamDrive(array(
30+
'name' => 'Project Resources'));
31+
$requestId = Uuid::uuid4()->toString();
32+
$teamDrive = $driveService->teamdrives->create($requestId, $teamDriveMetadata, array([
33+
'fields' => 'id']));
34+
printf("Team Drive ID: %s\n", $teamDrive->id);
35+
return $teamDrive->id;
36+
37+
} catch(Exception $e) {
38+
39+
echo "Error Message: ".$e;
40+
}
41+
42+
}
43+
// [END drive_create_team_drives]
44+
require_once 'vendor/autoload.php';
45+
createTeamDrive();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_download_file]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
function downloadFile()
21+
{
22+
try {
23+
24+
$client = new Client();
25+
$client->useApplicationDefaultCredentials();
26+
$client->addScope(Drive::DRIVE);
27+
$driveService = new Drive($client);
28+
$realFileId = readline("Enter File Id: ");
29+
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
30+
$fileId = $realFileId;
31+
$response = $driveService->files->get($fileId, array([
32+
'alt' => 'media']));
33+
$content = $response->getBody()->getContents();
34+
return $content;
35+
36+
} catch(Exception $e) {
37+
echo "Error Message: ".$e;
38+
}
39+
40+
}
41+
// [END drive_download_file]
42+
require_once 'vendor/autoload.php';
43+
downloadFile();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_export_pdf]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
function exportPdf()
21+
{
22+
try {
23+
$client = new Client();
24+
$client->useApplicationDefaultCredentials();
25+
$client->addScope(Drive::DRIVE);
26+
$driveService = new Drive($client);
27+
$realFileId = readline("Enter File Id: ");
28+
$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
29+
$fileId = $realFileId;
30+
$response = $driveService->files->export($fileId, 'application/pdf', array([
31+
'alt' => 'media']));
32+
$content = $response->getBody()->getContents();
33+
return $content;
34+
35+
} catch(Exception $e) {
36+
echo "Error Message: ".$e;
37+
}
38+
39+
}
40+
// [END drive_export_pdf]
41+
require_once 'vendor/autoload.php';
42+
exportPdf();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_fetch_appdata_folder]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
function fetchAppDataFolder()
21+
{
22+
try {
23+
$client = new Client();
24+
$client->useApplicationDefaultCredentials();
25+
$client->addScope(Drive::DRIVE);
26+
$client->addScope(Drive::DRIVE_APPDATA);
27+
$driveService = new Drive($client);
28+
$file = $driveService->files->get('appDataFolder', array([
29+
'fields' => 'id'
30+
]));
31+
printf("Folder ID: %s\n", $file->id);
32+
return $file->id;
33+
} catch (Exception $e) {
34+
echo "Error Message: ".$e;
35+
}
36+
}
37+
// [END drive_fetch_appdata_folder]
38+
require_once 'vendor/autoload.php';
39+
fetchAppDataFolder();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
// [START drive_fetch_changes]
18+
use Google\Client;
19+
use Google\Service\Drive;
20+
# TODO - PHP client currently chokes on fetching start page token
21+
function fetchChanges()
22+
{
23+
try {
24+
$client = new Client();
25+
$client->useApplicationDefaultCredentials();
26+
$client->addScope(Drive::DRIVE);
27+
$driveService = new Drive($client);
28+
# Begin with our last saved start token for this user or the
29+
# current token from getStartPageToken()
30+
$savedStartPageToken = readLine("Enter Start Page Token: ");
31+
$pageToken = $savedStartPageToken;
32+
while ($pageToken != null) {
33+
$response = $driveService->changes->listChanges($pageToken, array([
34+
'spaces' => 'drive'
35+
]));
36+
foreach ($response->changes as $change) {
37+
// Process change
38+
printf("Change found for file: %s", $change->fileId);
39+
}
40+
if ($response->newStartPageToken != null) {
41+
// Last page, save this token for the next polling interval
42+
$savedStartPageToken = $response->newStartPageToken;
43+
}
44+
$pageToken = $response->nextPageToken;
45+
}
46+
echo $savedStartPageToken;
47+
} catch(Exception $e) {
48+
echo "Error Message: ".$e;
49+
}
50+
51+
}
52+
require_once 'vendor/autoload.php';
53+
// [END drive_fetch_changes]
54+
fetchChanges();

0 commit comments

Comments
 (0)