The Google API Client for PHP has undergone major internal changes in 2.0
. Please read through
the list below in order to upgrade to the latest version:
Before
The project was cloned in your project and you would run the autoloader from wherever:
// the autoload file was included
require_once 'google-api-php-client/src/Google/autoload.php'; // or wherever autoload.php is located
// OR classes were added one-by-one
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
After
This library now uses composer (We suggest installing composer globally). Add this library by running the following in the root of your project:
$ composer require google/apiclient:~2.0
This will install this library and generate an autoload file in vendor/autoload.php
in the root
of your project. You can now include this library with the following code:
require_once 'vendor/autoload.php';
Before
$accessToken = $client->getAccessToken();
print_r($accessToken);
// would output:
// string(153) "{"access_token":"ya29.FAKsaByOPoddfzvKRo_LBpWWCpVTiAm4BjsvBwxtN7IgSNoUfcErBk_VPl4iAiE1ntb_","token_type":"Bearer","expires_in":3593,"created":1445548590}"
file_put_contents($credentialsPath, $accessToken);
After
$accessToken = $client->getAccessToken();
print_r($accessToken);
// will output:
// array(4) {
// ["access_token"]=>
// string(73) "ya29.FAKsaByOPoddfzvKRo_LBpWWCpVTiAm4BjsvBwxtN7IgSNoUfcErBk_VPl4iAiE1ntb_"
// ["token_type"]=>
// string(6) "Bearer"
// ["expires_in"]=>
// int(3593)
// ["created"]=>
// int(1445548590)
// }
file_put_contents($credentialsPath, json_encode($accessToken));
Before
$ticket = $client->verifyIdToken($idToken);
$data = $ticket->getAttributes();
$userId = $data['payload']['sub'];
After
$userData = $client->verifyIdToken($idToken);
$userId = $userData['sub'];
For service accounts, we now use setAuthConfig
or useApplicationDefaultCredentials
Before
$client_email = '1234567890-a1b2c3d4e5f6g7h8i@developer.gserviceaccount.com';
$private_key = file_get_contents('MyProject.p12');
$scopes = array('https://www.googleapis.com/auth/sqlservice.admin');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
After
$client->setAuthConfig('/path/to/service-account.json');
// OR use environment variables (recommended)
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client->useApplicationDefaultCredentials();
Note: P12s are deprecated in favor of service account JSON, which can be generated in the Credentials section of Google Developer Console.
In order to impersonate a user, call setSubject
when your service account
credentials are being used.
Before
$user_to_impersonate = 'user@example.org';
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
$user_to_impersonate,
);
After
$user_to_impersonate = 'user@example.org';
$client->setSubject($user_to_impersonate);
For App Engine authentication, we now use the underlying google/auth
and
call useApplicationDefaultCredentials
:
Before
$client->setAuth(new Google_Auth_AppIdentity($client));
$client->getAuth()
->authenticateForScope('https://www.googleapis.com/auth/sqlservice.admin')
After
$client->useDefaultApplicationCredentials();
$client->addScope('https://www.googleapis.com/auth/sqlservice.admin');
This will detect when the App Engine environment is present, and use the appropriate credentials.
google/auth
is now used for authentication. As a result, all
Google_Auth
-related functionality has been removed. The methods that were a part of
Google_Auth_Abstract
have been moved into the Google_Client
object.
Before
$request = new Google_Http_Request();
$client->getAuth()->sign($request);
After
$httpClient = new GuzzleHttp\Client();
$client->authorize($httpClient);
Before
$request = new Google_Http_Request();
$response = $client->getAuth()->authenticatedRequest($request);
After
$httpClient = new GuzzleHttp\Client();
$client->authorize($httpClient);
$request = $httpClient->createRequest('POST', $url);
$response = $httpClient->send($request);
In addition, other methods that were callable on Google_Auth_OAuth2
are now called
on the Google_Client
object:
Before
$client->getAuth()->refreshToken($token);
$client->getAuth()->refreshTokenWithAssertion();
$client->getAuth()->revokeToken($token);
$client->getAuth()->isAccessTokenExpired();
After
$client->refreshToken($token);
$client->refreshTokenWithAssertion();
$client->revokeToken($token);
$client->isAccessTokenExpired();
This was previously PHP 5.2
. If you still need to use PHP 5.2, please continue to use
the v1-master branch.
Guzzle 5
is used for all HTTP Requests. As a result, all Google_IO
-related
functionality and Google_Http
-related functionality has been changed or removed.
- Removed
Google_Http_Request
- Removed
Google_IO_Abstract
,Google_IO_Exception
,Google_IO_Curl
, andGoogle_IO_Stream
- Removed methods
Google_Client::getIo
andGoogle_Client::setIo
- Refactored
Google_Http_Batch
andGoogle_Http_MediaFileUpload
for Guzzle 5 - Added
Google_Http_Pool
for HTTP Pooling via Guzzle 5 - Added
Google_Client::getHttpClient
andGoogle_Client::setHttpClient
for getting and setting the Guzzle 5GuzzleHttp\ClientInterface
object.
PSR 3
LoggerInterface
is now supported, and Monolog is used for all logging. As a result, allGoogle_Logger
-related functionality has been removed:- Removed
Google_Logger_Abstract
,Google_Logger_Exception
,Google_Logger_File
,Google_Logger_Null
, andGoogle_Logger_Psr
Google_Client::setLogger
now requiresPsr\Log\LoggerInterface
- Removed
firebase/jwt
is now used for all JWT signing and verifying. As a result, the following classes have been changed or removed:- Removed
Google_Signer_P12
- Removed
Google_Verifier_Pem
- Removed
Google_Auth_LoginTicket
(see below)
- Removed
- The following classes and methods have been removed in favor of
google/auth
:- Removed methods
Google_Client::getAuth
andGoogle_Client::setAuth
- Removed
Google_Auth_Abstract
Google_Auth_Abstract::sign
andGoogle_Auth_Abstract::authenticatedRequest
have been replaced byGoogle_Client::authorize
. See the above examples for more details.
- Removed
Google_Auth_AppIdentity
. This is now supported ingoogle/auth
and is used automatically whenGoogle_Client::useApplicationDefaultCredentials
is called. - Removed
Google_Auth_AssertionCredentials
. UseGoogle_Client::setAuthConfig
instead. - Removed
Google_Auth_ComputeEngine
. This is now supported ingoogle/auth
, and is used automatically whenGoogle_Client::useApplicationDefaultCredentials
is called. - Removed
Google_Auth_Exception
- Removed
Google_Auth_LoginTicket
. Calls toGoogle_Client::verifyIdToken
now returns the payload of the ID Token as an array if the verification is successful. - Removed
Google_Auth_OAuth2
. This functionality is now supported ingoogle/auth
and wrapped inGoogle_Client
. These changes will only affect applications callingGoogle_Client::getAuth
, as the methods onGoogle_Client
have not changed. - Removed
Google_Auth_Simple
. This is now supported ingoogle/auth
and is used automatically whenGoogle_Client::setDeveloperKey
is called.
- Removed methods
Google_Client::sign
has been replaced byGoogle_Client::authorize
. This function now takes aGuzzleHttp\ClientInterface
object and uses the following decision tree for authentication:- Uses Application Default Credentials when
Google_Client::useApplicationDefaultCredentials
is called
- Looks for
GOOGLE_APPLICATION_CREDENTIALS
environment variable if set - Looks in
~/.config/gcloud/application_default_credentials.json
- Otherwise, uses
GCECredentials
- Uses API Key if set (see
Client::setDeveloperKey
) - Uses Access Token if set (call
Client::setAccessToken
) - Automatically refreshes access tokens if one is set and the access token is expired
- Uses Application Default Credentials when
- Removed
Google_Config
- Removed
Google_Utils
Google\Auth\CacheInterface
is used for all caching. As a result:- Removed
Google_Cache_Abstract
- Classes
Google_Cache_Apc
,Google_Cache_File
,Google_Cache_Memcache
, andGoogle_Cache_Null
now implementGoogle\Auth\CacheInterface
.
- Removed
- Removed
$boundary
constructor argument forGoogle_Http_MediaFileUpload