-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from RajeshGogo/driveV3-snippets
test: Drive v3 snippets
- Loading branch information
Showing
20 changed files
with
976 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.