From 6b35b40231c4ee487194d1471b9bca3c5534c3b6 Mon Sep 17 00:00:00 2001 From: Diego Soek Date: Sun, 29 May 2022 22:38:29 -0300 Subject: [PATCH] Implements Google Forms API --- forms/quickstart/README.md | 25 ++++++++++ forms/quickstart/composer.json | 5 ++ forms/quickstart/quickstart.php | 87 +++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 forms/quickstart/README.md create mode 100644 forms/quickstart/composer.json create mode 100644 forms/quickstart/quickstart.php diff --git a/forms/quickstart/README.md b/forms/quickstart/README.md new file mode 100644 index 0000000..2ce5c61 --- /dev/null +++ b/forms/quickstart/README.md @@ -0,0 +1,25 @@ +# Google Forms API Quickstart + +Complete the steps described in the [Google Forms API Quickstart](https://developers.google.com/forms/api/quickstart/php), and in about five minutes you'll have a simple PHP command-line application that makes requests to the Google Forms 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 +``` diff --git a/forms/quickstart/composer.json b/forms/quickstart/composer.json new file mode 100644 index 0000000..e4e8d86 --- /dev/null +++ b/forms/quickstart/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "google/apiclient": "^2.12.2" + } +} diff --git a/forms/quickstart/quickstart.php b/forms/quickstart/quickstart.php new file mode 100644 index 0000000..9203d8e --- /dev/null +++ b/forms/quickstart/quickstart.php @@ -0,0 +1,87 @@ +setApplicationName('Google Forms API PHP Quickstart'); + $client->setScopes(Google_Service_Forms::FORMS_BODY_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_Forms($client); + +// Prints the response link of the requested form: +// https://docs.google.com/forms/d/1UPwWDi4-oYVC7nPt_LISIc4jBnP5z4Z0qVUT4Rw5feg/edit +$formId = '1UPwWDi4-oYVC7nPt_LISIc4jBnP5z4Z0qVUT4Rw5feg'; +$form = $service->forms->get($formId); + +printf("The form response link is: %s\n", $form->getResponderUri()); +// [END form_quickstart]