private function get_token() { $client = new Google_Client(); if ($credentials_file = $this->checkServiceAccountCredentialsFile()) { $client->setAuthConfig($credentials_file); } elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { $client->useApplicationDefaultCredentials(); } else { echo missingServiceAccountDetailsWarning(); exit; } $client->setApplicationName("IARD Tables"); $client->setScopes(array('https://spreadsheets.google.com/feeds')); $token = $client->fetchAccessTokenWithAssertion(); return $token['access_token']; }
function getClient() { // Authenticate your API Client $client = new Google_Client(); //$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL); $client->addScope(Google_Service_Storage::DEVSTORAGE_READ_WRITE); // see ~/sandbox/zouk-event-calendar/vendor/google/apiclient/src/Google/Service/Storage.php $client->setAccessType("offline"); $client->useApplicationDefaultCredentials(); // no need to acquire special credentials $token = $client->getAccessToken(); if (!$token) { // this is always the case, and same access token is aquired in the fetch call below (can be printed) //syslog(LOG_DEBUG, "girish: access token not present"); $token = $client->fetchAccessTokenWithAssertion(); $client->setAccessToken($token); //syslog(LOG_DEBUG, $token['access_token']); } // token acquried above is always expired. and even if you run fetchAccess...Refreshtoken() it still stays expired //if ($client->isAccessTokenExpired()) { // //syslog(LOG_DEBUG, "girish: access token expired"); // $client->fetchAccessTokenWithRefreshToken($token); //} //if ($client->isAccessTokenExpired()) { // syslog(LOG_DEBUG, "girish: access token still expired!"); // no idea how this works //} return $client; }
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { // use the application default credentials $client->useApplicationDefaultCredentials(); } else { echo missingServiceAccountDetailsWarning(); exit; } $client->setApplicationName("Sheets API Testing"); $client->setScopes(['https://www.googleapis.com/auth/drive', 'https://spreadsheets.google.com/feeds']); // Some people have reported needing to use the following setAuthConfig command // which requires the email address of your service account (you can get that from the json file) // $client->setAuthConfig(["type" => "service_account", "client_email" => "*****@*****.**"]); // The file ID was copied from a URL while editing the sheet in Chrome $fileId = '1zni9j6L5zqq05F0ZjMK3gag5CatyXW8B4riQaTkWpjM'; // Access Token is used for Steps 2 and beyond $tokenArray = $client->fetchAccessTokenWithAssertion(); $accessToken = $tokenArray["access_token"]; // Section 1: Uncomment to get file metadata with the drive service // This is also the service that would be used to create a new spreadsheet file $service = new Google_Service_Drive($client); $results = $service->files->get($fileId); var_dump($results); // Section 2: Uncomment to get list of worksheets // $url = "https://spreadsheets.google.com/feeds/worksheets/$fileId/private/full"; // $method = 'GET'; // $headers = ["Authorization" => "Bearer $accessToken"]; // $httpClient = new GuzzleHttp\Client(['headers' => $headers]); // $resp = $httpClient->request($method, $url); // $body = $resp->getBody()->getContents(); // $code = $resp->getStatusCode(); // $reason = $resp->getReasonPhrase();