function service_account(&$client, $useremail = '*****@*****.**', $service = 'https://www.googleapis.com/auth/drive', $client_id = '1016393342084-dqql4goj9s0l402sbf4dtnoq2tsk0hp8.apps.googleusercontent.com', $service_account_name = '*****@*****.**') { global $CFG; include_once "{$CFG->dirroot}/google/google-api-php-client/examples/templates/base.php"; require_once "{$CFG->dirroot}/google/google-api-php-client/autoload.php"; //$key_file_location = "$CFG->dirroot/google/key.p12"; //key.p12 $key_file_location = $CFG->dirroot . '/google/key.p12'; if ($client_id == '<YOUR_CLIENT_ID>' || !strlen($service_account_name) || !strlen($key_file_location)) { echo missingServiceAccountDetailsWarning(); } $client->setApplicationName("Service_account"); if (isset($_SESSION['service_token'])) { unset($_SESSION['service_token']); // $client->setAccessToken($_SESSION['service_token']); } // $client->getAuth()->revokeToken($client->auth->token); $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials($service_account_name, array($service), $key, 'notasecret', 'http://oauth.net/grant_type/jwt/1.0/bearer', $useremail); $client->setAssertionCredentials($cred); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); return json_decode($client->getAccessToken()); }
function writeGoogleSpreadsheet($data) { if (empty($data)) { return FALSE; } session_start(); include_once "google-api-php-client/examples/templates/base.php"; require_once realpath(dirname(__FILE__) . '/google-api-php-client/src/Google/autoload.php'); $client_id = '76948799826-4u3c0b83kqcas2j034rivdsp2ik8qjp5.apps.googleusercontent.com'; $service_account_name = '*****@*****.**'; $key_file_location = 'FeedBackMail-de82c85c3106.p12'; if (strpos($client_id, "googleusercontent") == false || !strlen($service_account_name) || !strlen($key_file_location)) { echo missingServiceAccountDetailsWarning(); exit; } $client = new Google_Client(); $client->setApplicationName("FeedBackMail"); $service = new Google_Service_Drive($client); if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/drive', 'https://spreadsheets.google.com/feeds'), $key); $client->setAssertionCredentials($cred); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); $resultArray = json_decode($_SESSION['service_token']); $accessToken = $resultArray->access_token; $fileId = '1vlMTld652YHY0ey3NshxKjo7WriaGsv31dMpS8Fhp30'; $url = "https://spreadsheets.google.com/feeds/list/{$fileId}/od6/private/full"; $method = 'POST'; $headers = ["Authorization" => "Bearer {$accessToken}", 'Content-Type' => 'application/atom+xml']; $postBody = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">'; $postBody .= '<gsx:created>' . $data['date_created'] . '</gsx:created>'; $postBody .= '<gsx:fio>' . $data['fio'] . '</gsx:fio>'; $postBody .= '<gsx:email>' . $data['email'] . '</gsx:email>'; $postBody .= '<gsx:message>' . $data['message'] . '</gsx:message>'; $postBody .= '</entry>'; $req = new Google_Http_Request($url, $method, $headers, $postBody); $curl = new Google_IO_Curl($client); try { $curl->executeRequest($req); return TRUE; } catch (Exception $e) { return FALSE; } }
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']; }
public function __construct($course_account) { global $CFG; $this->private_key = file_get_contents($CFG->dirroot . '/repository/morsle/key.p12'); if (!$this->private_key) { throw new Exception("Key could not be loaded from file {$CFG->dirroot}.'/repository/morsle/key.p12'"); } $this->client_id = '1016393342084-dqql4goj9s0l402sbf4dtnoq2tsk0hp8.apps.googleusercontent.com'; $this->service_account = '*****@*****.**'; if (!strlen($this->service_account) || !strlen($this->private_key)) { echo missingServiceAccountDetailsWarning(); exit; } $this->scopes = array('https://www.googleapis.com/auth/calendar'); $this->credentials = new Google_Auth_AssertionCredentials($this->service_account, $this->scopes, $this->private_key, 'notasecret', 'http://oauth.net/grant_type/jwt/1.0/bearer', $course_account); $this->client = new Google_Client(); $this->client->setApplicationName("Service_account"); $this->client->setAssertionCredentials($this->credentials); if ($this->client->getAuth()->isAccessTokenExpired()) { try { $this->client->getAuth()->refreshTokenWithAssertion($this->credentials); } catch (Exception $e) { var_dump($e); } } $_SESSION['service_token'] = $this->client->getAccessToken(); //return json_decode($this->client->getAccessToken()); $this->morsle_calendar = new Google_Service_Calendar($this->client); }
accessible from the webserver! The name is the email address value provided as part of the service account (not your address!) Make sure the Books API is enabled on this account as well, or the call will fail. ************************************************/ $client_id = '<YOUR_CLIENT_ID>'; //Client ID $service_account_name = ''; //Email Address $key_file_location = ''; //key.p12 echo pageHeader("Service Account Access"); if (strpos($client_id, "googleusercontent") == false || !strlen($service_account_name) || !strlen($key_file_location)) { echo missingServiceAccountDetailsWarning(); exit; } $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $service = new Google_Service_Books($client); /************************************************ If we have an access token, we can carry on. Otherwise, we'll get one with the help of an assertion credential. In other examples the list of scopes was managed by the Client, but here we have to list them manually. We also supply the service account ************************************************/ if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']);
<?php include "vendor/autoload.php"; set_time_limit(0); error_reporting(E_ALL); ini_set("display_errors", "1"); if (!file_exists("config.php")) { throw new \Exception("No Config file found. Maybe copy example.config.php -> config.php"); } include "config.php"; if (strpos(CLIENT_ID, "googleusercontent") == false || !strlen(SERVICE_ACCOUNT_NAME) || !strlen(KEY_FILE_LOCATION)) { Output::error(missingServiceAccountDetailsWarning()); exit; } $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $service = new Google_Service_AndroidPublisher($client); /************************************************ * If we have an access token, we can carry on. * Otherwise, we'll get one with the help of an * assertion credential. In other examples the list * of scopes was managed by the Client, but here * we have to list them manually. We also supply * the service account ************************************************/ if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } $key = file_get_contents(KEY_FILE_LOCATION); $cred = new Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/androidpublisher'), $key); $client->setAssertionCredentials($cred);
function login_client() { $client_id = YOUR_CLIENT_ID; //Client ID $service_account_name = SERVICE_ACCOUNT_NAME; //Email Address $key_file_location = KEY_FILE_LOCATION; //key.p12 echo pageHeader("Service Account Access"); if (strpos($client_id, "googleusercontent") == false || !strlen($service_account_name) || !strlen($key_file_location)) { echo missingServiceAccountDetailsWarning(); exit; } $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples eli"); $client->addScope("https://www.googleapis.com/auth/fusiontables", "https://www.googleapis.com/auth/fusiontables.readonly", "https://www.googleapis.com/auth/drive"); /************************************************ If we have an access token, we can carry on. Otherwise, we'll get one with the help of an assertion credential. In other examples the list of scopes was managed by the Client, but here we have to list them manually. We also supply the service account ************************************************/ if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.apps.readonly', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/fusiontables', 'https://www.googleapis.com/auth/fusiontables.readonly'), $key); $client->setAssertionCredentials($cred); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); if ($client->getAccessToken()) { return $client; } else { return false; } //************************************************ }