Victor Hernandez e24c9e7c11 First commit | 4 anos atrás | |
---|---|---|
.. | ||
src | 4 anos atrás | |
tests | 4 anos atrás | |
.editorconfig | 4 anos atrás | |
.gitignore | 4 anos atrás | |
.php_cs | 4 anos atrás | |
.travis.yml | 4 anos atrás | |
CHANGELOG.md | 4 anos atrás | |
CONTRIBUTING.md | 4 anos atrás | |
COPYING | 4 anos atrás | |
LICENSE | 4 anos atrás | |
README.md | 4 anos atrás | |
autoload.php | 4 anos atrás | |
composer.json | 4 anos atrás | |
phpunit.xml.dist | 4 anos atrás |
This is Google’s officially supported PHP client library for using OAuth 2.0 authorization and authentication with Google APIs.
The recommended way to install the google auth library is through Composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version:
composer.phar require google/auth
This library provides an implementation of application default credentials for PHP.
The Application Default Credentials provide a simple way to get authorization credentials for use in calling Google APIs.
They are best suited for cases when the call needs to have the same identity and authorization level for the application independent of the user. This is the recommended approach to authorize calls to Cloud APIs, particularly when you’re building an application that uses Google Compute Engine.
To use Application Default Credentials
, You first need to download a set of
JSON credentials for your project. Go to APIs & Auth > Credentials in
the Google Developers Console and select
Service account from the Add credentials dropdown.
This file is your only copy of these credentials. It should never be committed with your source code, and should be stored securely.
Once downloaded, store the path to this file in the
GOOGLE_APPLICATION_CREDENTIALS
environment variable.
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
PHP’s
putenv
function is just one way to set an environment variable. Consider using.htaccess
or apache configuration files as well.
Before making your API call, you must be sure the API you’re calling has been
enabled. Go to APIs & Auth > APIs in the
Google Developers Console and enable the APIs you’d like to
call. For the example below, you must enable the Drive API
.
As long as you update the environment variable below to point to your JSON credentials file, the following code should output a list of your Drive files.
use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
// specify the path to your application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
// define the scopes for your API call
$scopes = ['https://www.googleapis.com/auth/drive.readonly'];
// create middleware
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
$stack = HandlerStack::create();
$stack->push($middleware);
// create the HTTP client
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://www.googleapis.com',
'auth' => 'google_auth' // authorize all requests
]);
// make the request
$response = $client->get('drive/v2/files');
// show the result!
print_r((string) $response->getBody());
If you are using Guzzle 5, replace the create middleware
and
create the HTTP Client
steps with the following:
// create the HTTP client
$client = new Client([
'base_url' => 'https://www.googleapis.com',
'auth' => 'google_auth' // authorize all requests
]);
// create subscriber
$subscriber = ApplicationDefaultCredentials::getSubscriber($scopes);
$client->getEmitter()->attach($subscriber);
This library is licensed under Apache 2.0. Full license text is available in COPYING.
See CONTRIBUTING.
Please report bugs at the project on Github. Don’t hesitate to ask questions about the client or APIs on StackOverflow.