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).

/gdwpm-api/Google_Client.php  
  1. class Google_Client { 
  2. /** 
  3. * @static 
  4. * @var Google_Auth $auth 
  5. */ 
  6. static $auth; 
  7.  
  8. /** 
  9. * @static 
  10. * @var Google_IO $io 
  11. */ 
  12. static $io; 
  13.  
  14. /** 
  15. * @static 
  16. * @var Google_Cache $cache 
  17. */ 
  18. static $cache; 
  19.  
  20. /** 
  21. * @static 
  22. * @var boolean $useBatch 
  23. */ 
  24. static $useBatch = false; 
  25.  
  26. /** @var array $scopes */ 
  27. protected $scopes = array(); 
  28.  
  29. /** @var bool $useObjects */ 
  30. protected $useObjects = false; 
  31.  
  32. // definitions of services that are discovered. 
  33. protected $services = array(); 
  34.  
  35. // Used to track authenticated state, can't discover services after doing authenticate() 
  36. private $authenticated = false; 
  37.  
  38. public function __construct($config = array()) { 
  39. global $gdwpm_apiConfig; 
  40. $gdwpm_apiConfig = array_merge($gdwpm_apiConfig, $config); 
  41. self::$cache = new $gdwpm_apiConfig['cacheClass'](); 
  42. self::$auth = new $gdwpm_apiConfig['authClass'](); 
  43. self::$io = new $gdwpm_apiConfig['ioClass'](); 
  44.  
  45. /** 
  46. * Add a service 
  47. */ 
  48. public function addService($service, $version = false) { 
  49. global $gdwpm_apiConfig; 
  50. if ($this->authenticated) { 
  51. throw new Google_Exception('Cant add services after having authenticated'); 
  52. $this->services[$service] = array(); 
  53. if (isset($gdwpm_apiConfig['services'][$service])) { 
  54. // Merge the service descriptor with the default values 
  55. $this->services[$service] = array_merge($this->services[$service], $gdwpm_apiConfig['services'][$service]); 
  56.  
  57. public function authenticate($code = null) { 
  58. $service = $this->prepareService(); 
  59. $this->authenticated = true; 
  60. return self::$auth->authenticate($service, $code); 
  61.  
  62. /** 
  63. * @return array 
  64. * @visible For Testing 
  65. */ 
  66. public function prepareService() { 
  67. $service = array(); 
  68. $scopes = array(); 
  69. if ($this->scopes) { 
  70. $scopes = $this->scopes; 
  71. } else { 
  72. foreach ($this->services as $key => $val) { 
  73. if (isset($val['scope'])) { 
  74. if (is_array($val['scope'])) { 
  75. $scopes = array_merge($val['scope'], $scopes); 
  76. } else { 
  77. $scopes[] = $val['scope']; 
  78. } else { 
  79. $scopes[] = 'https://www.googleapis.com/auth/' . $key; 
  80. unset($val['discoveryURI']); 
  81. unset($val['scope']); 
  82. $service = array_merge($service, $val); 
  83. $service['scope'] = implode(' ', $scopes); 
  84. return $service; 
  85.  
  86. /** 
  87. * Set the OAuth 2.0 access token using the string that resulted from calling authenticate() 
  88. * or Google_Client#getAccessToken(). 
  89. * @param string $accessToken JSON encoded string containing in the following format: 
  90. * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",  
  91. * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} 
  92. */ 
  93. public function setAccessToken($accessToken) { 
  94. if ($accessToken == null || 'null' == $accessToken) { 
  95. $accessToken = null; 
  96. self::$auth->setAccessToken($accessToken); 
  97.  
  98. /** 
  99. * Set the type of Auth class the client should use. 
  100. * @param string $authClassName 
  101. */ 
  102. public function setAuthClass($authClassName) { 
  103. self::$auth = new $authClassName(); 
  104.  
  105. /** 
  106. * Construct the OAuth 2.0 authorization request URI. 
  107. * @return string 
  108. */ 
  109. public function createAuthUrl() { 
  110. $service = $this->prepareService(); 
  111. return self::$auth->createAuthUrl($service['scope']); 
  112.  
  113. /** 
  114. * Get the OAuth 2.0 access token. 
  115. * @return string $accessToken JSON encoded string in the following format: 
  116. * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",  
  117. * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} 
  118. */ 
  119. public function getAccessToken() { 
  120. $token = self::$auth->getAccessToken(); 
  121. return (null == $token || 'null' == $token) ? null : $token; 
  122.  
  123. /** 
  124. * Returns if the access_token is expired. 
  125. * @return bool Returns True if the access_token is expired. 
  126. */ 
  127. public function isAccessTokenExpired() { 
  128. return self::$auth->isAccessTokenExpired(); 
  129.  
  130. /** 
  131. * Set the developer key to use, these are obtained through the API Console. 
  132. * @see http://code.google.com/apis/console-help/#generatingdevkeys 
  133. * @param string $developerKey 
  134. */ 
  135. public function setDeveloperKey($developerKey) { 
  136. self::$auth->setDeveloperKey($developerKey); 
  137.  
  138. /** 
  139. * Set OAuth 2.0 "state" parameter to achieve per-request customization. 
  140. * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 
  141. * @param string $state 
  142. */ 
  143. public function setState($state) { 
  144. self::$auth->setState($state); 
  145.  
  146. /** 
  147. * @param string $accessType Possible values for access_type include: 
  148. * {@code "offline"} to request offline access from the user. (This is the default value) 
  149. * {@code "online"} to request online access from the user. 
  150. */ 
  151. public function setAccessType($accessType) { 
  152. self::$auth->setAccessType($accessType); 
  153.  
  154. /** 
  155. * @param string $approvalPrompt Possible values for approval_prompt include: 
  156. * {@code "force"} to force the approval UI to appear. (This is the default value) 
  157. * {@code "auto"} to request auto-approval when possible. 
  158. */ 
  159. public function setApprovalPrompt($approvalPrompt) { 
  160. self::$auth->setApprovalPrompt($approvalPrompt); 
  161.  
  162. /** 
  163. * Set the application name, this is included in the User-Agent HTTP header. 
  164. * @param string $applicationName 
  165. */ 
  166. public function setApplicationName($applicationName) { 
  167. global $gdwpm_apiConfig; 
  168. $gdwpm_apiConfig['application_name'] = $applicationName; 
  169.  
  170. /** 
  171. * Set the OAuth 2.0 Client ID. 
  172. * @param string $clientId 
  173. */ 
  174. public function setClientId($clientId) { 
  175. global $gdwpm_apiConfig; 
  176. $gdwpm_apiConfig['oauth2_client_id'] = $clientId; 
  177. self::$auth->clientId = $clientId; 
  178.  
  179. /** 
  180. * Get the OAuth 2.0 Client ID. 
  181. */ 
  182. public function getClientId() { 
  183. return self::$auth->clientId; 
  184.  
  185. /** 
  186. * Set the OAuth 2.0 Client Secret. 
  187. * @param string $clientSecret 
  188. */ 
  189. public function setClientSecret($clientSecret) { 
  190. global $gdwpm_apiConfig; 
  191. $gdwpm_apiConfig['oauth2_client_secret'] = $clientSecret; 
  192. self::$auth->clientSecret = $clientSecret; 
  193.  
  194. /** 
  195. * Get the OAuth 2.0 Client Secret. 
  196. */ 
  197. public function getClientSecret() { 
  198. return self::$auth->clientSecret; 
  199.  
  200. /** 
  201. * Set the OAuth 2.0 Redirect URI. 
  202. * @param string $redirectUri 
  203. */ 
  204. public function setRedirectUri($redirectUri) { 
  205. global $gdwpm_apiConfig; 
  206. $gdwpm_apiConfig['oauth2_redirect_uri'] = $redirectUri; 
  207. self::$auth->redirectUri = $redirectUri; 
  208.  
  209. /** 
  210. * Get the OAuth 2.0 Redirect URI. 
  211. */ 
  212. public function getRedirectUri() { 
  213. return self::$auth->redirectUri; 
  214.  
  215. /** 
  216. * Fetches a fresh OAuth 2.0 access token with the given refresh token. 
  217. * @param string $refreshToken 
  218. * @return void 
  219. */ 
  220. public function refreshToken($refreshToken) { 
  221. self::$auth->refreshToken($refreshToken); 
  222.  
  223. /** 
  224. * Revoke an OAuth2 access token or refresh token. This method will revoke the current access 
  225. * token, if a token isn't provided. 
  226. * @throws Google_AuthException 
  227. * @param string|null $token The token (access token or a refresh token) that should be revoked. 
  228. * @return boolean Returns True if the revocation was successful, otherwise False. 
  229. */ 
  230. public function revokeToken($token = null) { 
  231. self::$auth->revokeToken($token); 
  232.  
  233. /** 
  234. * Verify an id_token. This method will verify the current id_token, if one 
  235. * isn't provided. 
  236. * @throws Google_AuthException 
  237. * @param string|null $token The token (id_token) that should be verified. 
  238. * @return Google_LoginTicket Returns an apiLoginTicket if the verification was 
  239. * successful. 
  240. */ 
  241. public function verifyIdToken($token = null) { 
  242. return self::$auth->verifyIdToken($token); 
  243.  
  244. /** 
  245. * @param Google_AssertionCredentials $creds 
  246. * @return void 
  247. */ 
  248. public function setAssertionCredentials(Google_AssertionCredentials $creds) { 
  249. self::$auth->setAssertionCredentials($creds); 
  250.  
  251. /** 
  252. * This function allows you to overrule the automatically generated scopes,  
  253. * so that you can ask for more or less permission in the auth flow 
  254. * Set this before you call authenticate() though! 
  255. * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/moderator') 
  256. */ 
  257. public function setScopes($scopes) { 
  258. $this->scopes = is_string($scopes) ? explode(" ", $scopes) : $scopes; 
  259.  
  260. /** 
  261. * Returns the list of scopes set on the client 
  262. * @return array the list of scopes 
  263. */ 
  264. public function getScopes() { 
  265. return $this->scopes; 
  266.  
  267. /** 
  268. * If 'plus.login' is included in the list of requested scopes, you can use 
  269. * this method to define types of app activities that your app will write. 
  270. * You can find a list of available types here: 
  271. * @link https://developers.google.com/+/api/moment-types 
  272. * @param array $requestVisibleActions Array of app activity types 
  273. */ 
  274. public function setRequestVisibleActions($requestVisibleActions) { 
  275. self::$auth->requestVisibleActions = 
  276. join(" ", $requestVisibleActions); 
  277.  
  278. /** 
  279. * Declare if objects should be returned by the api service classes. 
  280. * @param boolean $useObjects True if objects should be returned by the service classes. 
  281. * False if associative arrays should be returned (default behavior). 
  282. * @experimental 
  283. */ 
  284. public function setUseObjects($useObjects) { 
  285. global $gdwpm_apiConfig; 
  286. $gdwpm_apiConfig['use_objects'] = $useObjects; 
  287.  
  288. /** 
  289. * Declare if objects should be returned by the api service classes. 
  290. * @param boolean $useBatch True if the experimental batch support should 
  291. * be enabled. Defaults to False. 
  292. * @experimental 
  293. */ 
  294. public function setUseBatch($useBatch) { 
  295. self::$useBatch = $useBatch; 
  296.  
  297. /** 
  298. * @static 
  299. * @return Google_Auth the implementation of apiAuth. 
  300. */ 
  301. public static function getAuth() { 
  302. return Google_Client::$auth; 
  303.  
  304. /** 
  305. * @static 
  306. * @return Google_IO the implementation of apiIo. 
  307. */ 
  308. public static function getIo() { 
  309. return Google_Client::$io; 
  310.  
  311. /** 
  312. * @return Google_Cache the implementation of apiCache. 
  313. */ 
  314. public function getCache() { 
  315. return Google_Client::$cache;