The Google_Client setAssertionCredentials method is a function that allows developers to authenticate an application by providing an assertion credential, which is a signed JWT (JSON Web Token) or a Google-signed file. This method is typically used for server-to-server communication and is commonly used with the Google Cloud Platform.
Example 1: Server-to-server authentication with JWT
/** * This example shows how to set the assertion credentials for server-to-server * authentication using a JWT. */
use Google\Auth\Credentials\ServiceAccountCredentials; use Google\Auth\Middleware\AuthTokenMiddleware; use Google\Client;
// Initialize the Google_Client object $client = new Google_Client();
// Set the assertion credentials $credentials = new ServiceAccountCredentials( 'client_id', 'path/to/key.json' ); $client->setAssertionCredentials($credentials);
// Set the access token to the client object $middleware = new AuthTokenMiddleware($credentials); $client->setAccessTokenMiddleware($middleware);
Example 2: Server-to-server authentication with signed file
/** * This example shows how to set the assertion credentials for server-to-server * authentication using a Google-signed file. */
use Google\Auth\Credentials\ServiceAccountCredentials; use Google\Client;
// Initialize the Google_Client object $client = new Google_Client();
// Set the assertion credentials $credentials = new ServiceAccountCredentials( 'client_id', 'path/to/key.json', 'user_email_address', 'https://www.googleapis.com/auth/calendar' ); $client->setAssertionCredentials($credentials);
// Set the access token to the client object $client->setAccessToken($credentials->fetchAccessToken()[0]);
The package library used in these examples is the Google API PHP Client library. This library provides a simple interface to Google APIs and allows developers to easily authenticate and use Google services in their applications.
PHP Google_Client::setAssertionCredentials - 30 examples found. These are the top rated real world PHP examples of Google_Client::setAssertionCredentials extracted from open source projects. You can rate examples to help us improve the quality of examples.