From e79c7feaf64fa860bd8767626be8b6c13123080b Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Fri, 19 Aug 2016 02:45:52 -0700 Subject: [PATCH] Add Google Spreadsheet example --- examples/google_spreadsheet_values_update.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 examples/google_spreadsheet_values_update.php diff --git a/examples/google_spreadsheet_values_update.php b/examples/google_spreadsheet_values_update.php new file mode 100644 index 0000000..235b72f --- /dev/null +++ b/examples/google_spreadsheet_values_update.php @@ -0,0 +1,72 @@ + 'offline', + 'approval_prompt' => 'force', + 'client_id' => CLIENT_ID, + 'redirect_uri' => REDIRECT_URI, + 'response_type' => 'code', + 'scope' => 'https://www.googleapis.com/auth/spreadsheets', +)); +echo 'Open the following link in your browser:' . "\n"; +echo $auth_url . "\n"; +echo 'Enter verification code: '; +$code = trim(fgets(STDIN)); + +// Exchange authorization code for an access token. +$curl = new Curl(); +$curl->post(OAUTH2_TOKEN_URI, array( + 'client_id' => CLIENT_ID, + 'client_secret' => CLIENT_SECRET, + 'code' => $code, + 'grant_type' => 'authorization_code', + 'redirect_uri' => REDIRECT_URI, +)); +$access_token = $curl->response; + +// Update spreadsheet. +$spreadsheet_id = '1Z2cXhdG-K44KgSzHTcGhx1dY-xY31yuYGwX21F4GeUp'; +$range = 'Sheet1!A1'; +$url = 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheet_id . '/values/' . $range; +$url .= '?' . http_build_query(array( + 'valueInputOption' => 'USER_ENTERED', +)); + +$data = array( + 'values' => array( + array( + 'This is cell A1', + 'B1', + 'C1', + 'and D1', + ), + ), +); + +$curl = new Curl(); +$curl->setHeader('Content-Type', 'application/json'); +$curl->setHeader('Authorization', 'Bearer ' . $access_token->access_token); +$curl->put($url, $data); + +if ($curl->error) { + echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage; + var_dump($curl); +} else { + var_dump($curl->response); +}