Google_Client

The Google API Client http://code.google.com/p/google-api-php-client/.

Defined (1)

The class is defined in the following location(s).

/lib/vendor/google/apiclient/src/Google/Client.php  
  1. class Google_Client 
  2. const LIBVER = "1.0.6-beta"; 
  3. const USER_AGENT_SUFFIX = "google-api-php-client/"; 
  4. /** 
  5. * @var Google_Auth_Abstract $auth 
  6. */ 
  7. private $auth; 
  8.  
  9. /** 
  10. * @var Google_IO_Abstract $io 
  11. */ 
  12. private $io; 
  13.  
  14. /** 
  15. * @var Google_Cache_Abstract $cache 
  16. */ 
  17. private $cache; 
  18.  
  19. /** 
  20. * @var Google_Config $config 
  21. */ 
  22. private $config; 
  23.  
  24. /** 
  25. * @var boolean $deferExecution 
  26. */ 
  27. private $deferExecution = false; 
  28.  
  29. /** @var array $scopes */ 
  30. // Scopes requested by the client 
  31. protected $requestedScopes = array(); 
  32.  
  33. // definitions of services that are discovered. 
  34. protected $services = array(); 
  35.  
  36. // Used to track authenticated state, can't discover services after doing authenticate() 
  37. private $authenticated = false; 
  38.  
  39. /** 
  40. * Construct the Google Client. 
  41. * @param $config Google_Config or string for the ini file to load 
  42. */ 
  43. public function __construct($config = null) 
  44. if (is_string($config) && strlen($config)) { 
  45. $config = new Google_Config($config); 
  46. } else if ( !($config instanceof Google_Config)) { 
  47. $config = new Google_Config(); 
  48.  
  49. if ($this->isAppEngine()) { 
  50. // Automatically use Memcache if we're in AppEngine. 
  51. $config->setCacheClass('Google_Cache_Memcache'); 
  52.  
  53. if (version_compare(phpversion(), "5.3.4", "<=") || $this->isAppEngine()) { 
  54. // Automatically disable compress.zlib, as currently unsupported. 
  55. $config->setClassConfig('Google_Http_Request', 'disable_gzip', true); 
  56.  
  57. if ($config->getIoClass() == Google_Config::USE_AUTO_IO_SELECTION) { 
  58. if (function_exists('curl_version') && function_exists('curl_exec')) { 
  59. $config->setIoClass("Google_IO_Curl"); 
  60. } else { 
  61. $config->setIoClass("Google_IO_Stream"); 
  62.  
  63. $this->config = $config; 
  64.  
  65. /** 
  66. * Get a string containing the version of the library. 
  67. * @return string 
  68. */ 
  69. public function getLibraryVersion() 
  70. return self::LIBVER; 
  71.  
  72. /** 
  73. * Attempt to exchange a code for an valid authentication token. 
  74. * Helper wrapped around the OAuth 2.0 implementation. 
  75. * @param $code string code from accounts.google.com 
  76. * @return string token 
  77. */ 
  78. public function authenticate($code) 
  79. $this->authenticated = true; 
  80. return $this->getAuth()->authenticate($code); 
  81.  
  82. /** 
  83. * Set the auth config from the JSON string provided. 
  84. * This structure should match the file downloaded from 
  85. * the "Download JSON" button on in the Google Developer 
  86. * Console. 
  87. * @param string $json the configuration json 
  88. */ 
  89. public function setAuthConfig($json) 
  90. $data = json_decode($json); 
  91. $key = isset($data->installed) ? 'installed' : 'web'; 
  92. if (!isset($data->$key)) { 
  93. throw new Google_Exception("Invalid client secret JSON file."); 
  94. $this->setClientId($data->$key->client_id); 
  95. $this->setClientSecret($data->$key->client_secret); 
  96. if (isset($data->$key->redirect_uris)) { 
  97. $this->setRedirectUri($data->$key->redirect_uris[0]); 
  98.  
  99. /** 
  100. * Set the auth config from the JSON file in the path 
  101. * provided. This should match the file downloaded from 
  102. * the "Download JSON" button on in the Google Developer 
  103. * Console. 
  104. * @param string $file the file location of the client json 
  105. */ 
  106. public function setAuthConfigFile($file) 
  107. $this->setAuthConfig(file_get_contents($file)); 
  108.  
  109. /** 
  110. * @return array 
  111. * @visible For Testing 
  112. */ 
  113. public function prepareScopes() 
  114. if (empty($this->requestedScopes)) { 
  115. throw new Google_Auth_Exception("No scopes specified"); 
  116. $scopes = implode(' ', $this->requestedScopes); 
  117. return $scopes; 
  118.  
  119. /** 
  120. * Set the OAuth 2.0 access token using the string that resulted from calling createAuthUrl() 
  121. * or Google_Client#getAccessToken(). 
  122. * @param string $accessToken JSON encoded string containing in the following format: 
  123. * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",  
  124. * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} 
  125. */ 
  126. public function setAccessToken($accessToken) 
  127. if ($accessToken == 'null') { 
  128. $accessToken = null; 
  129. $this->getAuth()->setAccessToken($accessToken); 
  130.  
  131.  
  132.  
  133. /** 
  134. * Set the authenticator object 
  135. * @param Google_Auth_Abstract $auth 
  136. */ 
  137. public function setAuth(Google_Auth_Abstract $auth) 
  138. $this->config->setAuthClass(get_class($auth)); 
  139. $this->auth = $auth; 
  140.  
  141. /** 
  142. * Set the IO object 
  143. * @param Google_Io_Abstract $auth 
  144. */ 
  145. public function setIo(Google_Io_Abstract $io) 
  146. $this->config->setIoClass(get_class($io)); 
  147. $this->io = $io; 
  148.  
  149. /** 
  150. * Set the Cache object 
  151. * @param Google_Cache_Abstract $auth 
  152. */ 
  153. public function setCache(Google_Cache_Abstract $cache) 
  154. $this->config->setCacheClass(get_class($cache)); 
  155. $this->cache = $cache; 
  156.  
  157. /** 
  158. * Construct the OAuth 2.0 authorization request URI. 
  159. * @return string 
  160. */ 
  161. public function createAuthUrl() 
  162. $scopes = $this->prepareScopes(); 
  163. return $this->getAuth()->createAuthUrl($scopes); 
  164.  
  165. /** 
  166. * Get the OAuth 2.0 access token. 
  167. * @return string $accessToken JSON encoded string in the following format: 
  168. * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",  
  169. * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} 
  170. */ 
  171. public function getAccessToken() 
  172. $token = $this->getAuth()->getAccessToken(); 
  173. // The response is json encoded, so could be the string null. 
  174. // It is arguable whether this check should be here or lower 
  175. // in the library. 
  176. return (null == $token || 'null' == $token || '[]' == $token) ? null : $token; 
  177.  
  178. /** 
  179. * Get the OAuth 2.0 refresh token. 
  180. * @return string $refreshToken refresh token or null if not available 
  181. */ 
  182. public function getRefreshToken() 
  183. return $this->getAuth()->getRefreshToken(); 
  184.  
  185. /** 
  186. * Returns if the access_token is expired. 
  187. * @return bool Returns True if the access_token is expired. 
  188. */ 
  189. public function isAccessTokenExpired() 
  190. return $this->getAuth()->isAccessTokenExpired(); 
  191.  
  192. /** 
  193. * Set OAuth 2.0 "state" parameter to achieve per-request customization. 
  194. * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 
  195. * @param string $state 
  196. */ 
  197. public function setState($state) 
  198. $this->getAuth()->setState($state); 
  199.  
  200. /** 
  201. * @param string $accessType Possible values for access_type include: 
  202. * {@code "offline"} to request offline access from the user. 
  203. * {@code "online"} to request online access from the user. 
  204. */ 
  205. public function setAccessType($accessType) 
  206. $this->config->setAccessType($accessType); 
  207.  
  208. /** 
  209. * @param string $approvalPrompt Possible values for approval_prompt include: 
  210. * {@code "force"} to force the approval UI to appear. (This is the default value) 
  211. * {@code "auto"} to request auto-approval when possible. 
  212. */ 
  213. public function setApprovalPrompt($approvalPrompt) 
  214. $this->config->setApprovalPrompt($approvalPrompt); 
  215.  
  216. /** 
  217. * Set the login hint, email address or sub id. 
  218. * @param string $loginHint 
  219. */ 
  220. public function setLoginHint($loginHint) 
  221. $this->config->setLoginHint($loginHint); 
  222.  
  223. /** 
  224. * Set the application name, this is included in the User-Agent HTTP header. 
  225. * @param string $applicationName 
  226. */ 
  227. public function setApplicationName($applicationName) 
  228. $this->config->setApplicationName($applicationName); 
  229.  
  230. /** 
  231. * Set the OAuth 2.0 Client ID. 
  232. * @param string $clientId 
  233. */ 
  234. public function setClientId($clientId) 
  235. $this->config->setClientId($clientId); 
  236.  
  237. /** 
  238. * Set the OAuth 2.0 Client Secret. 
  239. * @param string $clientSecret 
  240. */ 
  241. public function setClientSecret($clientSecret) 
  242. $this->config->setClientSecret($clientSecret); 
  243.  
  244. /** 
  245. * Set the OAuth 2.0 Redirect URI. 
  246. * @param string $redirectUri 
  247. */ 
  248. public function setRedirectUri($redirectUri) 
  249. $this->config->setRedirectUri($redirectUri); 
  250.  
  251. /** 
  252. * If 'plus.login' is included in the list of requested scopes, you can use 
  253. * this method to define types of app activities that your app will write. 
  254. * You can find a list of available types here: 
  255. * @link https://developers.google.com/+/api/moment-types 
  256. * @param array $requestVisibleActions Array of app activity types 
  257. */ 
  258. public function setRequestVisibleActions($requestVisibleActions) 
  259. if (is_array($requestVisibleActions)) { 
  260. $requestVisibleActions = join(" ", $requestVisibleActions); 
  261. $this->config->setRequestVisibleActions($requestVisibleActions); 
  262.  
  263. /** 
  264. * Set the developer key to use, these are obtained through the API Console. 
  265. * @see http://code.google.com/apis/console-help/#generatingdevkeys 
  266. * @param string $developerKey 
  267. */ 
  268. public function setDeveloperKey($developerKey) 
  269. $this->config->setDeveloperKey($developerKey); 
  270.  
  271. /** 
  272. * Set the hd (hosted domain) parameter streamlines the login process for 
  273. * Google Apps hosted accounts. By including the domain of the user, you 
  274. * restrict sign-in to accounts at that domain. 
  275. * @param $hd string - the domain to use. 
  276. */ 
  277. public function setHostedDomain($hd) 
  278. $this->config->setHostedDomain($hd); 
  279.  
  280. /** 
  281. * Set the prompt hint. Valid values are none, consent and select_account. 
  282. * If no value is specified and the user has not previously authorized 
  283. * access, then the user is shown a consent screen. 
  284. * @param $prompt string 
  285. */ 
  286. public function setPrompt($prompt) 
  287. $this->config->setPrompt($prompt); 
  288.  
  289. /** 
  290. * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth 
  291. * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which 
  292. * an authentication request is valid. 
  293. * @param $realm string - the URL-space to use. 
  294. */ 
  295. public function setOpenidRealm($realm) 
  296. $this->config->setOpenidRealm($realm); 
  297.  
  298. /** 
  299. * If this is provided with the value true, and the authorization request is 
  300. * granted, the authorization will include any previous authorizations 
  301. * granted to this user/application combination for other scopes. 
  302. * @param $include boolean - the URL-space to use. 
  303. */ 
  304. public function setIncludeGrantedScopes($include) 
  305. $this->config->setIncludeGrantedScopes($include); 
  306.  
  307. /** 
  308. * Fetches a fresh OAuth 2.0 access token with the given refresh token. 
  309. * @param string $refreshToken 
  310. * @return void 
  311. */ 
  312. public function refreshToken($refreshToken) 
  313. return $this->getAuth()->refreshToken($refreshToken); 
  314.  
  315. /** 
  316. * Revoke an OAuth2 access token or refresh token. This method will revoke the current access 
  317. * token, if a token isn't provided. 
  318. * @throws Google_Auth_Exception 
  319. * @param string|null $token The token (access token or a refresh token) that should be revoked. 
  320. * @return boolean Returns True if the revocation was successful, otherwise False. 
  321. */ 
  322. public function revokeToken($token = null) 
  323. return $this->getAuth()->revokeToken($token); 
  324.  
  325. /** 
  326. * Verify an id_token. This method will verify the current id_token, if one 
  327. * isn't provided. 
  328. * @throws Google_Auth_Exception 
  329. * @param string|null $token The token (id_token) that should be verified. 
  330. * @return Google_Auth_LoginTicket Returns an apiLoginTicket if the verification was 
  331. * successful. 
  332. */ 
  333. public function verifyIdToken($token = null) 
  334. return $this->getAuth()->verifyIdToken($token); 
  335.  
  336. /** 
  337. * Verify a JWT that was signed with your own certificates. 
  338. * @param $jwt the token 
  339. * @param $certs array of certificates 
  340. * @param $required_audience the expected consumer of the token 
  341. * @param [$issuer] the expected issues, defaults to Google 
  342. * @param [$max_expiry] the max lifetime of a token, defaults to MAX_TOKEN_LIFETIME_SECS 
  343. * @return token information if valid, false if not 
  344. */ 
  345. public function verifySignedJwt($id_token, $cert_location, $audience, $issuer, $max_expiry = null) 
  346. $auth = new Google_Auth_OAuth2($this); 
  347. $certs = $auth->retrieveCertsFromLocation($cert_location); 
  348. return $auth->verifySignedJwtWithCerts($id_token, $certs, $audience, $issuer, $max_expiry); 
  349.  
  350. /** 
  351. * @param Google_Auth_AssertionCredentials $creds 
  352. * @return void 
  353. */ 
  354. public function setAssertionCredentials(Google_Auth_AssertionCredentials $creds) 
  355. $this->getAuth()->setAssertionCredentials($creds); 
  356.  
  357. /** 
  358. * Set the scopes to be requested. Must be called before createAuthUrl(). 
  359. * Will remove any previously configured scopes. 
  360. * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.login',  
  361. * 'https://www.googleapis.com/auth/moderator') 
  362. */ 
  363. public function setScopes($scopes) 
  364. $this->requestedScopes = array(); 
  365. $this->addScope($scopes); 
  366.  
  367. /** 
  368. * This functions adds a scope to be requested as part of the OAuth2.0 flow. 
  369. * Will append any scopes not previously requested to the scope parameter. 
  370. * A single string will be treated as a scope to request. An array of strings 
  371. * will each be appended. 
  372. * @param $scope_or_scopes string|array e.g. "profile" 
  373. */ 
  374. public function addScope($scope_or_scopes) 
  375. if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { 
  376. $this->requestedScopes[] = $scope_or_scopes; 
  377. } else if (is_array($scope_or_scopes)) { 
  378. foreach ($scope_or_scopes as $scope) { 
  379. $this->addScope($scope); 
  380.  
  381. /** 
  382. * Returns the list of scopes requested by the client 
  383. * @return array the list of scopes 
  384. */ 
  385. public function getScopes() 
  386. return $this->requestedScopes; 
  387.  
  388. /** 
  389. * Declare whether batch calls should be used. This may increase throughput 
  390. * by making multiple requests in one connection. 
  391. * @param boolean $useBatch True if the batch support should 
  392. * be enabled. Defaults to False. 
  393. */ 
  394. public function setUseBatch($useBatch) 
  395. // This is actually an alias for setDefer. 
  396. $this->setDefer($useBatch); 
  397.  
  398. /** 
  399. * Declare whether making API calls should make the call immediately, or 
  400. * return a request which can be called with ->execute(); 
  401. * @param boolean $defer True if calls should not be executed right away. 
  402. */ 
  403. public function setDefer($defer) 
  404. $this->deferExecution = $defer; 
  405.  
  406. /** 
  407. * Helper method to execute deferred HTTP requests. 
  408. * @return object of the type of the expected class or array. 
  409. */ 
  410. public function execute($request) 
  411. if ($request instanceof Google_Http_Request) { 
  412. $request->setUserAgent( 
  413. $this->getApplicationName() 
  414. . " " . self::USER_AGENT_SUFFIX 
  415. . $this->getLibraryVersion() 
  416. ); 
  417. if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) { 
  418. $request->enableGzip(); 
  419. $request->maybeMoveParametersToBody(); 
  420. return Google_Http_REST::execute($this, $request); 
  421. } else if ($request instanceof Google_Http_Batch) { 
  422. return $request->execute(); 
  423. } else { 
  424. throw new Google_Exception("Do not know how to execute this type of object."); 
  425.  
  426. /** 
  427. * Whether or not to return raw requests 
  428. * @return boolean 
  429. */ 
  430. public function shouldDefer() 
  431. return $this->deferExecution; 
  432.  
  433. /** 
  434. * @return Google_Auth_Abstract Authentication implementation 
  435. */ 
  436. public function getAuth() 
  437. if (!isset($this->auth)) { 
  438. $class = $this->config->getAuthClass(); 
  439. $this->auth = new $class($this); 
  440. return $this->auth; 
  441.  
  442. /** 
  443. * @return Google_IO_Abstract IO implementation 
  444. */ 
  445. public function getIo() 
  446. if (!isset($this->io)) { 
  447. $class = $this->config->getIoClass(); 
  448. $this->io = new $class($this); 
  449. return $this->io; 
  450.  
  451. /** 
  452. * @return Google_Cache_Abstract Cache implementation 
  453. */ 
  454. public function getCache() 
  455. if (!isset($this->cache)) { 
  456. $class = $this->config->getCacheClass(); 
  457. $this->cache = new $class($this); 
  458. return $this->cache; 
  459.  
  460. /** 
  461. * Retrieve custom configuration for a specific class. 
  462. * @param $class string|object - class or instance of class to retrieve 
  463. * @param $key string optional - key to retrieve 
  464. */ 
  465. public function getClassConfig($class, $key = null) 
  466. if (!is_string($class)) { 
  467. $class = get_class($class); 
  468. return $this->config->getClassConfig($class, $key); 
  469.  
  470. /** 
  471. * Set configuration specific to a given class. 
  472. * $config->setClassConfig('Google_Cache_File',  
  473. * array('directory' => '/tmp/cache')); 
  474. * @param $class The class name for the configuration 
  475. * @param $config string key or an array of configuration values 
  476. * @param $value optional - if $config is a key, the value 
  477. */ 
  478. public function setClassConfig($class, $config, $value = null) 
  479. if (!is_string($class)) { 
  480. $class = get_class($class); 
  481. return $this->config->setClassConfig($class, $config, $value); 
  482.  
  483.  
  484. /** 
  485. * @return string the base URL to use for calls to the APIs 
  486. */ 
  487. public function getBasePath() 
  488. return $this->config->getBasePath(); 
  489.  
  490. /** 
  491. * @return string the name of the application 
  492. */ 
  493. public function getApplicationName() 
  494. return $this->config->getApplicationName(); 
  495.  
  496. /** 
  497. * Are we running in Google AppEngine? 
  498. * return bool 
  499. */ 
  500. public function isAppEngine() 
  501. return (isset($_SERVER['SERVER_SOFTWARE']) && 
  502. strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false);