This command-line application shows how to authenticate to Google Cloud APIs using different methods. This sample uses Storage as an example, but these methods will work on any Google Cloud API.
- Enable APIs - Enable the Storage API and create a new project or select an existing project.
- Download The Credentials - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
and select "Service Account Key". Create a new service account, use the JSON key type, and
select "Create". Once downloaded, set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON key that was downloaded. - Clone the repo and cd into this directory
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
$ cd php-docs-samples/auth
- Install dependencies via Composer.
Run
php composer.phar install --no-dev
(if composer is installed locally) orcomposer install --no-dev
(if composer is installed globally). - Run
php auth.php
. The following commands are available and work on command line:
auth-cloud-implicit Authenticate to a cloud client library using a service account implicitly.
auth-cloud-explicit Authenticate to a cloud client library using a service account explicitly.
auth-api-implicit Authenticate to a cloud API using a service account implicitly.
auth-api-explicit Authenticate to a cloud API using a service account explicitly.
auth-http-implicit Authenticate to a cloud API with HTTP using a service account implicitly.
auth-http-explicit Authenticate to a cloud API with HTTP using a service account explicitly.
- The following commands are available but will throw a ServiceException when run from command-line. The Compute Engine method only works on Compute Engine, App Engine Flexible, Cloud Functions, and Container Engine. The App Engine method only works on App Engine Standard.
auth-cloud-explicit-compute-engine Authenticate to a cloud client library using Compute Engine credentials explicitly.
auth-cloud-explicit-app-engine Authenticate to a cloud client library using App Engine Standard credentials explicitly.
auth-api-explicit-compute-engine Authenticate to a cloud API using Compute Engine credentials explicitly.
auth-api-explicit-app-engine Authenticate to a cloud API using App Engine Standard credentials explicitly.
-
You can test the samples that use Compute Engine / App Engine credentials by deploying to either App Engine Flexible (which allows usage of Compute Engine credentials since App Engine Flexible apps run on Compute Engine instances) or App Engine Standard. Run either
gcloud app deploy app-standard.yaml
orgcloud app deploy app-flex.yaml
. -
Run
php auth.php COMMAND --help
to print information about the usage of each command.
- See CONTRIBUTING.md
- See LICENSE