From f5e8f688156e2e0813f3171d43f9d5e4ad266370 Mon Sep 17 00:00:00 2001 From: asrivas Date: Wed, 20 Feb 2019 20:55:25 -0500 Subject: [PATCH 1/3] Add PHP Docs Quickstart --- docs/quickstart/README.md | 25 +++++++++++++++++++++++++ docs/quickstart/composer.json | 5 +++++ docs/quickstart/quickstart.php | 0 3 files changed, 30 insertions(+) create mode 100644 docs/quickstart/README.md create mode 100644 docs/quickstart/composer.json create mode 100644 docs/quickstart/quickstart.php diff --git a/docs/quickstart/README.md b/docs/quickstart/README.md new file mode 100644 index 0000000..6591310 --- /dev/null +++ b/docs/quickstart/README.md @@ -0,0 +1,25 @@ +# Google Docs API Quickstart + +Complete the steps described in the [Google Docs API Quickstart](https://developers.google.com/docs/api/quickstart/php), and in about five minutes you'll have a simple PHP command-line application that makes requests to the Google Docs API. + +## Set up + +### Install Composer Globally + +Before running this quickstart, be sure you have [Composer installed globally](https://getcomposer.org/doc/00-intro.md#globally). + +```sh +composer install +``` + +### Download Developer Credentials + +- Follow the steps in the quickstart instructions to download your developer + credentials and save them in a file called `credentails.json` in this + directory. + +## Run + +```sh +php quickstart.php +``` \ No newline at end of file diff --git a/docs/quickstart/composer.json b/docs/quickstart/composer.json new file mode 100644 index 0000000..8cb5960 --- /dev/null +++ b/docs/quickstart/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "google/apiclient": "^2.2" + } +} \ No newline at end of file diff --git a/docs/quickstart/quickstart.php b/docs/quickstart/quickstart.php new file mode 100644 index 0000000..e69de29 From 7a38aeb635d0838c34b62cab3a2f62838c544a77 Mon Sep 17 00:00:00 2001 From: sanjuktaghosh7 Date: Wed, 8 Jun 2022 21:05:39 +0530 Subject: [PATCH 2/3] Update in quickstart --- admin-sdk/directory/quickstart.php | 39 ++++++++++-------- admin-sdk/reports/quickstart.php | 20 ++++----- admin-sdk/reseller/quickstart.php | 18 ++++----- apps-script/quickstart/quickstart.php | 47 +++++++++++++--------- calendar/quickstart/quickstart.php | 47 +++++++++++++--------- classroom/quickstart/quickstart.php | 34 ++++++++++------ drive/activity-v2/quickstart.php | 8 ++-- drive/quickstart/quickstart.php | 58 +++++++++++++++------------ gmail/quickstart/quickstart.php | 31 ++++++++------ people/quickstart/quickstart.php | 41 +++++++++++-------- sheets/quickstart/quickstart.php | 33 +++++++++------ slides/quickstart/quickstart.php | 27 ++++++++----- tasks/quickstart/quickstart.php | 33 +++++++++------ vault/quickstart/quickstart.php | 34 ++++++++++------ 14 files changed, 275 insertions(+), 195 deletions(-) diff --git a/admin-sdk/directory/quickstart.php b/admin-sdk/directory/quickstart.php index 37e3b16..9d7b595 100644 --- a/admin-sdk/directory/quickstart.php +++ b/admin-sdk/directory/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('G Suite Directory API PHP Quickstart'); - $client->setScopes(Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY); + $client->setScopes('https://www.googleapis.com/auth/admin.directory.user.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -80,20 +80,27 @@ function getClient() $service = new Google_Service_Directory($client); // Print the first 10 users in the domain. -$optParams = array( - 'customer' => 'my_customer', - 'maxResults' => 10, - 'orderBy' => 'email', -); -$results = $service->users->listUsers($optParams); +try{ -if (count($results->getUsers()) == 0) { - print "No users found.\n"; -} else { - print "Users:\n"; - foreach ($results->getUsers() as $user) { - printf("%s (%s)\n", $user->getPrimaryEmail(), - $user->getName()->getFullName()); - } + $optParams = array( + 'customer' => 'my_customer', + 'maxResults' => 10, + 'orderBy' => 'email', + ); + $results = $service->users->listUsers($optParams); + + if (count($results->getUsers()) == 0) { + print "No users found.\n"; + } else { + print "Users:\n"; + foreach ($results->getUsers() as $user) { + printf("%s (%s)\n", $user->getPrimaryEmail(), + $user->getName()->getFullName()); + } + } +} +catch(Exception $e) { +// TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); } -// [END admin_sdk_directory_quickstart] +// [END admin_sdk_directory_quickstart] \ No newline at end of file diff --git a/admin-sdk/reports/quickstart.php b/admin-sdk/reports/quickstart.php index 00783ba..b9f507c 100644 --- a/admin-sdk/reports/quickstart.php +++ b/admin-sdk/reports/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Reports API PHP Quickstart'); - $client->setScopes(Google_Service_Reports::ADMIN_REPORTS_AUDIT_READONLY); + $client->setScopes('https://www.googleapis.com/auth/admin.reports.audit.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -83,19 +83,19 @@ function getClient() $userKey = 'all'; $applicationName = 'login'; $optParams = array( - 'maxResults' => 10, + 'maxResults' => 10, ); $results = $service->activities->listActivities( $userKey, $applicationName, $optParams); if (count($results->getItems()) == 0) { - print "No logins found.\n"; + print "No logins found.\n"; } else { - print "Logins:\n"; - foreach ($results->getItems() as $activity) { - printf("%s: %s (%s)\n", $activity->getId()->getTime(), - $activity->getActor()->getEmail(), - $activity->getEvents()[0]->getName()); - } + print "Logins:\n"; + foreach ($results->getItems() as $activity) { + printf("%s: %s (%s)\n", $activity->getId()->getTime(), + $activity->getActor()->getEmail(), + $activity->getEvents()[0]->getName()); + } } -// [END admin_sdk_reports_quickstart] +// [END admin_sdk_reports_quickstart] \ No newline at end of file diff --git a/admin-sdk/reseller/quickstart.php b/admin-sdk/reseller/quickstart.php index ef6e712..a2cb096 100644 --- a/admin-sdk/reseller/quickstart.php +++ b/admin-sdk/reseller/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('G Suite Reseller API PHP Quickstart'); - $client->setScopes(Google_Service_Reseller::APPS_ORDER); + $client->setScopes("https://www.googleapis.com/auth/apps.order"); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -81,17 +81,17 @@ function getClient() // Print the first 10 subscriptions you manage. $optParams = array( - 'maxResults' => 10, + 'maxResults' => 10, ); $results = $service->subscriptions->listSubscriptions($optParams); if (count($results->getSubscriptions()) == 0) { - print "No subscriptions found.\n"; + print "No subscriptions found.\n"; } else { - print "Subscriptions:\n"; - foreach ($results->getSubscriptions() as $subscription) { - printf("%s (%s, %s)\n", $subscription->getCustomerId(), - $subscription->getSkuId(), $subscription->getPlan()->getPlanName()); - } + print "Subscriptions:\n"; + foreach ($results->getSubscriptions() as $subscription) { + printf("%s (%s, %s)\n", $subscription->getCustomerId(), + $subscription->getSkuId(), $subscription->getPlan()->getPlanName()); + } } -// [END admin_sdk_reseller_quickstart] +// [END admin_sdk_reseller_quickstart] \ No newline at end of file diff --git a/apps-script/quickstart/quickstart.php b/apps-script/quickstart/quickstart.php index 0491c61..8e51c25 100644 --- a/apps-script/quickstart/quickstart.php +++ b/apps-script/quickstart/quickstart.php @@ -85,37 +85,44 @@ function getClient() $service = new Google_Service_Script($client); // Create a management request object. -$request = new Google_Service_Script_CreateProjectRequest(); -$request->setTitle('My Script'); -$response = $service->projects->create($request); +try{ -$scriptId = $response->getScriptId(); + $request = new Google_Service_Script_CreateProjectRequest(); + $request->setTitle('My Script'); + $response = $service->projects->create($request); -$code = <<getScriptId(); + + $code = <<setName('hello'); -$file1->setType('SERVER_JS'); -$file1->setSource($code); + $file1 = new Google_Service_Script_ScriptFile(); + $file1->setName('hello'); + $file1->setType('SERVER_JS'); + $file1->setSource($code); -$manifest = <<setName('appsscript'); -$file2->setType('JSON'); -$file2->setSource($manifest); + $file2 = new Google_Service_Script_ScriptFile(); + $file2->setName('appsscript'); + $file2->setType('JSON'); + $file2->setSource($manifest); -$request = new Google_Service_Script_Content(); -$request->setScriptId($scriptId); -$request->setFiles([$file1, $file2]); + $request = new Google_Service_Script_Content(); + $request->setScriptId($scriptId); + $request->setFiles([$file1, $file2]); -$response = $service->projects->updateContent($scriptId, $request); -echo "https://script.google.com/d/" . $response->getScriptId() . "/edit\n"; -// [END apps_script_api_quickstart] + $response = $service->projects->updateContent($scriptId, $request); + echo "https://script.google.com/d/" . $response->getScriptId() . "/edit\n"; +} +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); +} +// [END apps_script_api_quickstart] \ No newline at end of file diff --git a/calendar/quickstart/quickstart.php b/calendar/quickstart/quickstart.php index a5c6f43..bee8cc7 100644 --- a/calendar/quickstart/quickstart.php +++ b/calendar/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Calendar API PHP Quickstart'); - $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY); + $client->setScopes('https://www.googleapis.com/auth/calendar.events.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -80,26 +80,33 @@ function getClient() $service = new Google_Service_Calendar($client); // Print the next 10 events on the user's calendar. -$calendarId = 'primary'; -$optParams = array( - 'maxResults' => 10, - 'orderBy' => 'startTime', - 'singleEvents' => true, - 'timeMin' => date('c'), -); -$results = $service->events->listEvents($calendarId, $optParams); -$events = $results->getItems(); +try{ -if (empty($events)) { - print "No upcoming events found.\n"; -} else { - print "Upcoming events:\n"; - foreach ($events as $event) { - $start = $event->start->dateTime; - if (empty($start)) { - $start = $event->start->date; + $calendarId = 'primary'; + $optParams = array( + 'maxResults' => 10, + 'orderBy' => 'startTime', + 'singleEvents' => true, + 'timeMin' => date('c'), + ); + $results = $service->events->listEvents($calendarId, $optParams); + $events = $results->getItems(); + + if (empty($events)) { + print "No upcoming events found.\n"; + } else { + print "Upcoming events:\n"; + foreach ($events as $event) { + $start = $event->start->dateTime; + if (empty($start)) { + $start = $event->start->date; + } + printf("%s (%s)\n", $event->getSummary(), $start); } - printf("%s (%s)\n", $event->getSummary(), $start); } } -// [END calendar_quickstart] +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); +} +// [END calendar_quickstart] \ No newline at end of file diff --git a/classroom/quickstart/quickstart.php b/classroom/quickstart/quickstart.php index 9808c12..f245ef3 100644 --- a/classroom/quickstart/quickstart.php +++ b/classroom/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Classroom API PHP Quickstart'); - $client->setScopes(Google_Service_Classroom::CLASSROOM_COURSES_READONLY); + $client->setScopes('https://www.googleapis.com/auth/classroom.courses.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -80,17 +80,25 @@ function getClient() $service = new Google_Service_Classroom($client); // Print the first 10 courses the user has access to. -$optParams = array( - 'pageSize' => 10 -); -$results = $service->courses->listCourses($optParams); +try{ -if (count($results->getCourses()) == 0) { - print "No courses found.\n"; -} else { - print "Courses:\n"; - foreach ($results->getCourses() as $course) { - printf("%s (%s)\n", $course->getName(), $course->getId()); - } + $optParams = array( + 'pageSize' => 10 + ); + $results = $service->courses->listCourses($optParams); + + if (count($results->getCourses()) == 0) { + print "No courses found.\n"; + } else { + print "Courses:\n"; + foreach ($results->getCourses() as $course) { + printf("%s (%s)\n", $course->getName(), $course->getId()); + } + } } -// [END classroom_quickstart] +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); +} + +// [END classroom_quickstart] \ No newline at end of file diff --git a/drive/activity-v2/quickstart.php b/drive/activity-v2/quickstart.php index 38d754d..5f5af6c 100644 --- a/drive/activity-v2/quickstart.php +++ b/drive/activity-v2/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Drive Activity API Quickstart'); - $client->setScopes(Google_Service_DriveActivity::DRIVE_ACTIVITY_READONLY); + $client->setScopes('https://www.googleapis.com/auth/drive.activity.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -157,8 +157,8 @@ function getTargetInfo($target) if ($target->getDriveItem() != null) { return sprintf('driveItem:"%s"', $target->getDriveItem()->getTitle()); } - if ($target->getTeamDrive() != null) { - return sprintf('teamDrive:"%s"', $target->getTeamDrive()->getTitle()); + if ($target->getDrive() != null) { + return sprintf('drive:"%s"', $target->getDrive()->getTitle()); } if ($target->getFileComment() != null) { $parent = $target->getFileComment()->getParent(); @@ -169,4 +169,4 @@ function getTargetInfo($target) } return getOneOf($target); } -// [END drive_activity_v2_quickstart] +// [END drive_activity_v2_quickstart] \ No newline at end of file diff --git a/drive/quickstart/quickstart.php b/drive/quickstart/quickstart.php index 80ac0c4..a6e90a1 100644 --- a/drive/quickstart/quickstart.php +++ b/drive/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Drive API PHP Quickstart'); - $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY); + $client->setScopes('https://www.googleapis.com/auth/drive.metadata.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -45,31 +45,39 @@ function getClient() } // If there is no previous token or it's expired. - if ($client->isAccessTokenExpired()) { - // Refresh the token if possible, else fetch a new one. - if ($client->getRefreshToken()) { - $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); - } else { - // Request authorization from the user. - $authUrl = $client->createAuthUrl(); - printf("Open the following link in your browser:\n%s\n", $authUrl); - print 'Enter verification code: '; - $authCode = trim(fgets(STDIN)); + try{ - // Exchange authorization code for an access token. - $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); - $client->setAccessToken($accessToken); - // Check to see if there was an error. - if (array_key_exists('error', $accessToken)) { - throw new Exception(join(', ', $accessToken)); + if ($client->isAccessTokenExpired()) { + // Refresh the token if possible, else fetch a new one. + if ($client->getRefreshToken()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); + + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + $client->setAccessToken($accessToken); + + // Check to see if there was an error. + if (array_key_exists('error', $accessToken)) { + throw new Exception(join(', ', $accessToken)); + } } + // Save the token to a file. + if (!file_exists(dirname($tokenPath))) { + mkdir(dirname($tokenPath), 0700, true); + } + file_put_contents($tokenPath, json_encode($client->getAccessToken())); } - // Save the token to a file. - if (!file_exists(dirname($tokenPath))) { - mkdir(dirname($tokenPath), 0700, true); - } - file_put_contents($tokenPath, json_encode($client->getAccessToken())); + } + catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Some error occured: '.$e->getMessage(); } return $client; } @@ -81,8 +89,8 @@ function getClient() // Print the names and IDs for up to 10 files. $optParams = array( - 'pageSize' => 10, - 'fields' => 'nextPageToken, files(id, name)' + 'pageSize' => 10, + 'fields' => 'nextPageToken, files(id, name)' ); $results = $service->files->listFiles($optParams); @@ -94,4 +102,4 @@ function getClient() printf("%s (%s)\n", $file->getName(), $file->getId()); } } -// [END drive_quickstart] +// [END drive_quickstart] \ No newline at end of file diff --git a/gmail/quickstart/quickstart.php b/gmail/quickstart/quickstart.php index 4c9ad26..10f2ce6 100644 --- a/gmail/quickstart/quickstart.php +++ b/gmail/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Gmail API PHP Quickstart'); - $client->setScopes(Google_Service_Gmail::GMAIL_READONLY); + $client->setScopes('https://www.googleapis.com/auth/gmail.addons.current.message.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -79,16 +79,23 @@ function getClient() $client = getClient(); $service = new Google_Service_Gmail($client); -// Print the labels in the user's account. -$user = 'me'; -$results = $service->users_labels->listUsersLabels($user); +try{ -if (count($results->getLabels()) == 0) { - print "No labels found.\n"; -} else { - print "Labels:\n"; - foreach ($results->getLabels() as $label) { - printf("- %s\n", $label->getName()); - } + // Print the labels in the user's account. + $user = 'me'; + $results = $service->users_labels->listUsersLabels($user); + + if (count($results->getLabels()) == 0) { + print "No labels found.\n"; + } else { + print "Labels:\n"; + foreach ($results->getLabels() as $label) { + printf("- %s\n", $label->getName()); + } + } +} +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); } -// [END gmail_quickstart] +// [END gmail_quickstart] \ No newline at end of file diff --git a/people/quickstart/quickstart.php b/people/quickstart/quickstart.php index 4099488..d300072 100644 --- a/people/quickstart/quickstart.php +++ b/people/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('People API PHP Quickstart'); - $client->setScopes(Google_Service_PeopleService::CONTACTS_READONLY); + $client->setScopes('https://www.googleapis.com/auth/contacts.other.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -80,24 +80,31 @@ function getClient() $service = new Google_Service_PeopleService($client); // Print the names for up to 10 connections. -$optParams = array( - 'pageSize' => 10, - 'personFields' => 'names,emailAddresses', -); -$results = $service->people_connections->listPeopleConnections('people/me', $optParams); +try{ -if (count($results->getConnections()) == 0) { - print "No connections found.\n"; -} else { - print "People:\n"; - foreach ($results->getConnections() as $person) { - if (count($person->getNames()) == 0) { - print "No names found for this connection\n"; + $optParams = array( + 'pageSize' => 10, + 'personFields' => 'names,emailAddresses', + ); + $results = $service->people_connections->listPeopleConnections('people/me', $optParams); + + if (count($results->getConnections()) == 0) { + print "No connections found.\n"; } else { - $names = $person->getNames(); - $name = $names[0]; - printf("%s\n", $name->getDisplayName()); + print "People:\n"; + foreach ($results->getConnections() as $person) { + if (count($person->getNames()) == 0) { + print "No names found for this connection\n"; + } else { + $names = $person->getNames(); + $name = $names[0]; + printf("%s\n", $name->getDisplayName()); + } + } } - } +} +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); } // [END people_quickstart] diff --git a/sheets/quickstart/quickstart.php b/sheets/quickstart/quickstart.php index 4e83c17..7434946 100644 --- a/sheets/quickstart/quickstart.php +++ b/sheets/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Sheets API PHP Quickstart'); - $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY); + $client->setScopes('https://www.googleapis.com/auth/spreadsheets'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -81,18 +81,25 @@ function getClient() // Prints the names and majors of students in a sample spreadsheet: // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit -$spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'; -$range = 'Class Data!A2:E'; -$response = $service->spreadsheets_values->get($spreadsheetId, $range); -$values = $response->getValues(); +try{ -if (empty($values)) { - print "No data found.\n"; -} else { - print "Name, Major:\n"; - foreach ($values as $row) { - // Print columns A and E, which correspond to indices 0 and 4. - printf("%s, %s\n", $row[0], $row[4]); + $spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'; + $range = 'Class Data!A2:E'; + $response = $service->spreadsheets_values->get($spreadsheetId, $range); + $values = $response->getValues(); + + if (empty($values)) { + print "No data found.\n"; + } else { + print "Name, Major:\n"; + foreach ($values as $row) { + // Print columns A and E, which correspond to indices 0 and 4. + printf("%s, %s\n", $row[0], $row[4]); + } } } -// [END sheets_quickstart] +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); +} +// [END sheets_quickstart] \ No newline at end of file diff --git a/slides/quickstart/quickstart.php b/slides/quickstart/quickstart.php index 5832186..048f7e0 100644 --- a/slides/quickstart/quickstart.php +++ b/slides/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Slides API PHP Quickstart'); - $client->setScopes(Google_Service_Slides::PRESENTATIONS_READONLY); + $client->setScopes("https://www.googleapis.com/auth/presentations"); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -81,14 +81,21 @@ function getClient() // Prints the number of slides and elements in a sample presentation: // https://docs.google.com/presentation/d/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc/edit -$presentationId = '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc'; -$presentation = $service->presentations->get($presentationId); -$slides = $presentation->getSlides(); +try{ -printf("The presentation contains %s slides:\n", count($slides)); -foreach ($slides as $i => $slide) { - // Print columns A and E, which correspond to indices 0 and 4. - printf("- Slide #%s contains %s elements.\n", $i + 1, - count($slide->getPageElements())); + $presentationId = '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc'; + $presentation = $service->presentations->get($presentationId); + $slides = $presentation->getSlides(); + + printf("The presentation contains %s slides:\n", count($slides)); + foreach ($slides as $i => $slide) { + // Print columns A and E, which correspond to indices 0 and 4. + printf("- Slide #%s contains %s elements.\n", $i + 1, + count($slide->getPageElements())); + } +} +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); } -// [END slides_quickstart] +// [END slides_quickstart] \ No newline at end of file diff --git a/tasks/quickstart/quickstart.php b/tasks/quickstart/quickstart.php index e35cc63..a75f8bc 100644 --- a/tasks/quickstart/quickstart.php +++ b/tasks/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Tasks API PHP Quickstart'); - $client->setScopes(Google_Service_Tasks::TASKS_READONLY); + $client->setScopes('https://www.googleapis.com/auth/tasks.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -80,17 +80,24 @@ function getClient() $service = new Google_Service_Tasks($client); // Print the first 10 task lists. -$optParams = array( - 'maxResults' => 10, -); -$results = $service->tasklists->listTasklists($optParams); +try{ -if (count($results->getItems()) == 0) { - print "No task lists found.\n"; -} else { - print "Task lists:\n"; - foreach ($results->getItems() as $tasklist) { - printf("%s (%s)\n", $tasklist->getTitle(), $tasklist->getId()); - } + $optParams = array( + 'maxResults' => 10, + ); + $results = $service->tasklists->listTasklists($optParams); + + if (count($results->getItems()) == 0) { + print "No task lists found.\n"; + } else { + print "Task lists:\n"; + foreach ($results->getItems() as $tasklist) { + printf("%s (%s)\n", $tasklist->getTitle(), $tasklist->getId()); + } + } +} +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); } -// [END tasks_quickstart] +// [END tasks_quickstart] \ No newline at end of file diff --git a/vault/quickstart/quickstart.php b/vault/quickstart/quickstart.php index 16c3e7f..66e6957 100644 --- a/vault/quickstart/quickstart.php +++ b/vault/quickstart/quickstart.php @@ -29,7 +29,7 @@ function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Vault API PHP Quickstart'); - $client->setScopes(Google_Service_Vault::EDISCOVERY_READONLY); + $client->setScopes('https://www.googleapis.com/auth/ediscovery.readonly'); $client->setAuthConfig('credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); @@ -80,17 +80,25 @@ function getClient() $service = new Google_Service_Vault($client); // Print the first 10 matters. -$optParams = array( - 'pageSize' => 10 -); -$results = $service->matters->listMatters($optParams); +try{ -if (count($results->getMatters()) == 0) { - print "No matters found.\n"; -} else { - print "Matters:\n"; - foreach ($results->getMatters() as $matter) { - printf("%s (%s)\n", $matter->getName(), $matter->getMatterId()); - } + $optParams = array( + 'pageSize' => 10 + ); + $results = $service->matters->listMatters($optParams); + + if (count($results->getMatters()) == 0) { + print "No matters found.\n"; + } else { + print "Matters:\n"; + foreach ($results->getMatters() as $matter) { + printf("%s (%s)\n", $matter->getName(), $matter->getMatterId()); + } + } + +} +catch(Exception $e) { + // TODO(developer) - handle error appropriately + echo 'Message: ' .$e->getMessage(); } -// [END vault_quickstart] +// [END vault_quickstart] \ No newline at end of file From bb0281dce3240fad5ca189779f9d148646d74103 Mon Sep 17 00:00:00 2001 From: sanjuktaghosh7 Date: Wed, 8 Jun 2022 21:08:16 +0530 Subject: [PATCH 3/3] Update in quickstart --- docs/quickstart/quickstart.php | 87 ++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/docs/quickstart/quickstart.php b/docs/quickstart/quickstart.php index e69de29..daaf85e 100644 --- a/docs/quickstart/quickstart.php +++ b/docs/quickstart/quickstart.php @@ -0,0 +1,87 @@ +setApplicationName('Google Docs API PHP Quickstart'); + $client->setScopes('https://www.googleapis.com/auth/documents.readonly '); + $client->setAuthConfig('credentials.json'); + $client->setAccessType('offline'); + + // Load previously authorized credentials from a file. + $credentialsPath = expandHomeDirectory('token.json'); + if (file_exists($credentialsPath)) { + $accessToken = json_decode(file_get_contents($credentialsPath), true); + } else { + // Request authorization from the user. + $authUrl = $client->createAuthUrl(); + printf("Open the following link in your browser:\n%s\n", $authUrl); + print 'Enter verification code: '; + $authCode = trim(fgets(STDIN)); + + // Exchange authorization code for an access token. + $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); + + // Store the credentials to disk. + if (!file_exists(dirname($credentialsPath))) { + mkdir(dirname($credentialsPath), 0700, true); + } + file_put_contents($credentialsPath, json_encode($accessToken)); + printf("Credentials saved to %s\n", $credentialsPath); + } + $client->setAccessToken($accessToken); + + // Refresh the token if it's expired. + if ($client->isAccessTokenExpired()) { + $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); + file_put_contents($credentialsPath, json_encode($client->getAccessToken())); + } + return $client; +} + +/** + * Expands the home directory alias '~' to the full path. + * @param string $path the path to expand. + * @return string the expanded path. + */ +function expandHomeDirectory($path) +{ + $homeDirectory = getenv('HOME'); + if (empty($homeDirectory)) { + $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); + } + return str_replace('~', realpath($homeDirectory), $path); +} + +// Get the API client and construct the service object. +$client = getClient(); +$service = new Google_Service_Docs($client); + +// Prints the title of the requested doc: +// https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit +$documentId = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'; +$doc = $service->documents->get($documentId); + +printf("The document title is: %s\n", $doc->getTitle()); +// [END docs_quickstart] \ No newline at end of file