PHP code example of keboola / google-client-bundle

1. Go to this page and download the library: Download keboola/google-client-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keboola / google-client-bundle example snippets


use Keboola\Google\ClientBundle\Google\RestApi;

$api = new RestApi($logger); // Optional logger parameter
$api->setAppCredentials($clientId, $clientSecret);
$api->setCredentials($accessToken, $refreshToken);

// Get authorization URL
$authUrl = $api->getAuthorizationUrl(
    'http://localhost/callback',
    'https://www.googleapis.com/auth/drive.readonly',
    'force',
    'offline'
);

// Authorize using code
$tokens = $api->authorize($code, 'http://localhost/callback');

// API calls
$response = $api->request('/drive/v2/files');

use Keboola\Google\ClientBundle\Google\RestApi;

$api = RestApi::createWithOAuth(
    $clientId,
    $clientSecret,
    $accessToken,
    $refreshToken
);

// Usage same as above
$response = $api->request('/drive/v2/files');

use Keboola\Google\ClientBundle\Google\RestApi;

// Service account JSON configuration (from Google Cloud Console)
$serviceAccountConfig = [
    'type' => 'service_account',
    'project_id' => 'your-project-id',
    'private_key_id' => 'key-id',
    'private_key' => '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n',
    'client_email' => '[email protected]',
    'client_id' => '123456789',
    'auth_uri' => 'https://accounts.google.com/o/oauth2/auth',
    'token_uri' => 'https://oauth2.googleapis.com/token',
    'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs',
    'client_x509_cert_url' => 'https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com'
];

// Scope definitions
$scopes = [
    'https://www.googleapis.com/auth/cloud-platform',
    'https://www.googleapis.com/auth/drive.readonly'
];

// Create API client
$api = RestApi::createWithServiceAccount(
    $serviceAccountConfig,
    $scopes
);

// API calls
$response = $api->request('/drive/v2/files');

use Keboola\Google\ClientBundle\Google\RestApi;

// Load from JSON file
$serviceAccountConfig = json_decode(
    file_get_contents('/path/to/service-account-key.json'),
    true
);

$scopes = ['https://www.googleapis.com/auth/cloud-platform'];

$api = RestApi::createWithServiceAccount($serviceAccountConfig, $scopes);
$response = $api->request('/your-api-endpoint');

$api->setBackoffsCount(10); // Number of retries
$api->setDelayFn(function($retries) {
    return 1000 * pow(2, $retries); // Exponential backoff
});

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('google-api');
$logger->pushHandler(new StreamHandler('php://stdout'));

$api = RestApi::createWithServiceAccount(
    $serviceAccountConfig,
    $scopes,
    $logger
);

$response = $api->request('/endpoint', 'POST', [
    'Content-Type' => 'application/json'
], [
    'json' => ['key' => 'value'],
    'timeout' => 30
]);