3636class Google_Client
3737{
3838 const LIBVER = "1.0.1-alpha " ;
39+ const USER_AGENT_SUFFIX = "google-api-php-client/ " ;
3940 /**
4041 * @var Google_Auth_Abstract $auth
4142 */
@@ -61,9 +62,6 @@ class Google_Client
6162 */
6263 private $ deferExecution = false ;
6364
64- // Scopes available for the added services
65- protected $ availableScopes = array ();
66-
6765 /** @var array $scopes */
6866 // Scopes requested by the client
6967 protected $ requestedScopes = array ();
@@ -100,18 +98,6 @@ function_exists('date_default_timezone_set')) {
10098 $ this ->config = $ config ;
10199 }
102100
103- /**
104- * Adds the scopes available for a service
105- */
106- public function addService ($ service , $ version = false , $ availableScopes = array ())
107- {
108- if ($ this ->authenticated ) {
109- throw new Google_Exception ('Cant add services after having authenticated ' );
110- } else {
111- $ this ->availableScopes [$ service ] = $ availableScopes ;
112- }
113- }
114-
115101 /**
116102 * Get a string containing the version of the library.
117103 *
@@ -121,6 +107,16 @@ public function getLibraryVersion()
121107 {
122108 return self ::LIBVER ;
123109 }
110+
111+ /**
112+ * Shim function until templates are updated.
113+ * @todo(ianbarber): remove this.
114+ * @deprecated
115+ */
116+ public function addService ($ a , $ b , $ c )
117+ {
118+ return ;
119+ }
124120
125121 /**
126122 * Attempt to exchange a code for an valid authentication token.
@@ -175,16 +171,14 @@ public function setAuthConfigFile($file)
175171 public function prepareScopes ()
176172 {
177173 if (empty ($ this ->requestedScopes )) {
178- foreach ($ this ->availableScopes as $ service => $ serviceScopes ) {
179- array_push ($ this ->requestedScopes , $ serviceScopes [0 ]);
180- }
174+ throw new Google_Auth_Exception ("No scopes specified " );
181175 }
182176 $ scopes = implode (' ' , $ this ->requestedScopes );
183177 return $ scopes ;
184178 }
185179
186180 /**
187- * Set the OAuth 2.0 access token using the string that resulted from calling authenticate ()
181+ * Set the OAuth 2.0 access token using the string that resulted from calling createAuthUrl ()
188182 * or Google_Client#getAccessToken().
189183 * @param string $accessToken JSON encoded string containing in the following format:
190184 * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
@@ -418,15 +412,33 @@ public function setAssertionCredentials(Google_Auth_AssertionCredentials $creds)
418412 }
419413
420414 /**
421- * This function allows you to overrule the automatically generated scopes,
422- * so that you can ask for more or less permission in the auth flow
423- * Set this before you call authenticate() though!
424- * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.me',
415+ * Set the scopes to be requested. Must be called before createAuthUrl().
416+ * Will remove any previously configured scopes.
417+ * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.login',
425418 * 'https://www.googleapis.com/auth/moderator')
426419 */
427420 public function setScopes ($ scopes )
428421 {
429- $ this ->requestedScopes = is_string ($ scopes ) ? explode (" " , $ scopes ) : $ scopes ;
422+ $ this ->requestedScopes = array ();
423+ $ this ->addScope ($ scopes );
424+ }
425+
426+ /**
427+ * This functions adds a scope to be requested as part of the OAuth2.0 flow.
428+ * Will append any scopes not previously requested to the scope parameter.
429+ * A single string will be treated as a scope to request. An array of strings
430+ * will each be appended.
431+ * @param $scope_or_scopes string|array e.g. "profile"
432+ */
433+ public function addScope ($ scope_or_scopes )
434+ {
435+ if (is_string ($ scope_or_scopes ) && !in_array ($ scope_or_scopes , $ this ->requestedScopes )) {
436+ $ this ->requestedScopes [] = $ scope_or_scopes ;
437+ } else if (is_array ($ scope_or_scopes )) {
438+ foreach ($ scope_or_scopes as $ scope ) {
439+ $ this ->addScope ($ scope );
440+ }
441+ }
430442 }
431443
432444 /**
@@ -462,6 +474,28 @@ public function setDefer($defer)
462474 {
463475 $ this ->deferExecution = $ defer ;
464476 }
477+
478+ /**
479+ * Helper method to execute deferred HTTP requests.
480+ *
481+ * @returns object of the type of the expected class or array.
482+ */
483+ public function execute ($ request )
484+ {
485+ if ($ request instanceof Google_Http_Request) {
486+ $ request ->setUserAgent (
487+ $ this ->getApplicationName ()
488+ . " " . self ::USER_AGENT_SUFFIX
489+ . $ this ->getLibraryVersion ()
490+ );
491+ $ request ->maybeMoveParametersToBody ();
492+ return Google_Http_REST::execute ($ this , $ request );
493+ } else if ($ request instanceof Google_Http_Batch) {
494+ return $ request ->execute ();
495+ } else {
496+ throw new Google_Exception ("Do not know how to execute this type of object. " );
497+ }
498+ }
465499
466500 /**
467501 * Whether or not to return raw requests
0 commit comments