private function create_client() { $full_creds_file = $this->api_creds_file; if (!file_exists($full_creds_file)) { throw new \Exception("Cannot find file " . $full_creds_file, 1); } $client = new \Google_Client(); echo "Credentials File: " . $full_creds_file . PHP_EOL; $cred = $client->loadServiceAccountJson($full_creds_file, array(\Google_Service_Analytics::ANALYTICS_READONLY)); // Setup authentication to Google. // This is needed because we are running via a phar file $client->setClassConfig("Google_IO_Curl", ["options" => [CURLOPT_CAINFO => $this->pem_file]]); // re-auth if needed if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } return $client; }
/** * Create a configured Google Client ready for Datastore use, using the JSON service file from Google Dev Console * * @param $str_json_file * @return \Google_Client */ public static function createClientFromJson($str_json_file) { $obj_client = new \Google_Client(); $obj_client->setAssertionCredentials($obj_client->loadServiceAccountJson($str_json_file, [\Google_Service_Datastore::DATASTORE, \Google_Service_Datastore::USERINFO_EMAIL])); // App Engine php55 runtime dev server problems... $obj_client->setClassConfig('Google_Http_Request', 'disable_gzip', TRUE); return $obj_client; }
private function setGoogleServiceBigquery() { $client = new Google_Client(); $client->setApplicationName($this->application_name); $client->setClientId($this->client_id); $key = file_get_contents('privatekey.p12'); $cred = $client->loadServiceAccountJson($this->service_account_name, "https://www.googleapis.com/auth/bigquery"); /* $cred = new Google_Auth_AssertionCredentials( $this->service_account_name, array('https://www.googleapis.com/auth/bigquery'), $key); */ $client->setAssertionCredentials($cred); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); $service_token = $client->getAccessToken(); } $this->service = new Google_Service_Bigquery($client); // Google_Service_Bigquery }
<?php include "vendor/autoload.php"; include "config.php"; $client = new Google_Client(); $credentials = $client->loadServiceAccountJson(__DIR__ . "/googlekeys.json", Google_Service_Calendar::CALENDAR_READONLY); $client->setAssertionCredentials($credentials); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion(); } $service = new Google_Service_Calendar($client); // Print the next 10 events on the user's calendar. $calendarId = CAL_ID; $optParams = ['maxResults' => 15, 'orderBy' => 'startTime', 'singleEvents' => true, 'timeMin' => date('c')]; //$calendars = $service->calendarList->listCalendarList(); ///** @type Google_Service_Calendar_Calendar $cal */ //foreach($calendars->getItems() as $cal) { // var_dump($cal->getId()); //} $results = $service->events->listEvents($calendarId, $optParams); //var_dump($results->count()); $events = []; /** @type Google_Service_Calendar_Event $event */ foreach ($results as $event) { //var_dump($event); // var_dump($event->getSummary()); $time = $event->getStart()->getDateTime() ? new DateTime($event->getStart()->getDateTime()) : new DateTime($event->getStart()->getDate()); $start = $event->getStart()->getDateTime() ? $time->format('g:ia | D jS') : $time->format('D jS'); $events[] = ['summary' => $event->getSummary(), 'start' => $start, 'end' => $event->getEnd()->getDateTime(), 'location' => $event->getLocation()]; } header('Content-Type: application/json');
public function testServiceAccountJson() { $client = new Google_Client(); $c = $client->loadServiceAccountJson(__DIR__ . "/testdata/service-12345.json", array()); $this->assertInstanceOf('Google_Auth_AssertionCredentials', $c); }
* * @license MIT * @author Mike Funk <*****@*****.**> */ require __DIR__ . '/vendor/autoload.php'; use Google\Spreadsheet\DefaultServiceRequest; use Google\Spreadsheet\ServiceRequestFactory; use Google\Spreadsheet\SpreadsheetService; use Google\Spreadsheet\Exception as SpreadsheetException; // ensure we have a service account json file if (!file_exists(__DIR__ . '/service_account.json')) { throw new RuntimeException('First download your service account json from the google developer ' . 'console into service_account.json'); } // get the access token through the client $client = new Google_Client(); $credentials = $client->loadServiceAccountJson(__DIR__ . '/service_account.json', $scopes = ['https://spreadsheets.google.com/feeds']); $client->setAssertionCredentials($credentials); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion(); } $response = json_decode($client->getAuth()->getAccessToken()); $accessToken = $response->access_token; try { // bootstrap $serviceRequest = new DefaultServiceRequest($accessToken); ServiceRequestFactory::setInstance($serviceRequest); $spreadsheetService = new SpreadsheetService(); // get the spreadsheet $spreadsheetFeed = $spreadsheetService->getSpreadsheets(); // all spreadsheets // var_dump($spreadsheetFeed); exit;