|
1 | | -# Keboola Google API Client |
| 1 | +# Keboola Google Client Bundle |
2 | 2 |
|
3 | | -Basic client for Google APIs |
| 3 | +Keboola Google API Client with OAuth 2.0 and Service Account authentication support. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +composer require keboola/google-client-bundle |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +This library supports two types of authentication: |
| 14 | + |
| 15 | +1. **OAuth 2.0** - for applications that need access to user data |
| 16 | +2. **Service Account** - for server-to-server communication without user intervention |
| 17 | + |
| 18 | +### OAuth 2.0 Authentication |
| 19 | + |
| 20 | +#### Classic approach |
| 21 | + |
| 22 | +```php |
| 23 | +use Keboola\Google\ClientBundle\Google\RestApi; |
| 24 | + |
| 25 | +$api = new RestApi($logger); // Optional logger parameter |
| 26 | +$api->setAppCredentials($clientId, $clientSecret); |
| 27 | +$api->setCredentials($accessToken, $refreshToken); |
| 28 | + |
| 29 | +// Get authorization URL |
| 30 | +$authUrl = $api->getAuthorizationUrl( |
| 31 | + 'http://localhost/callback', |
| 32 | + 'https://www.googleapis.com/auth/drive.readonly', |
| 33 | + 'force', |
| 34 | + 'offline' |
| 35 | +); |
| 36 | + |
| 37 | +// Authorize using code |
| 38 | +$tokens = $api->authorize($code, 'http://localhost/callback'); |
| 39 | + |
| 40 | +// API calls |
| 41 | +$response = $api->request('/drive/v2/files'); |
| 42 | +``` |
| 43 | + |
| 44 | +#### New factory approach |
| 45 | + |
| 46 | +```php |
| 47 | +use Keboola\Google\ClientBundle\Google\RestApi; |
| 48 | + |
| 49 | +$api = RestApi::createWithOAuth( |
| 50 | + $clientId, |
| 51 | + $clientSecret, |
| 52 | + $accessToken, |
| 53 | + $refreshToken |
| 54 | +); |
| 55 | + |
| 56 | +// Usage same as above |
| 57 | +$response = $api->request('/drive/v2/files'); |
| 58 | +``` |
| 59 | + |
| 60 | +### Service Account Authentication |
| 61 | + |
| 62 | +```php |
| 63 | +use Keboola\Google\ClientBundle\Google\RestApi; |
| 64 | + |
| 65 | +// Service account JSON configuration (from Google Cloud Console) |
| 66 | +$serviceAccountConfig = [ |
| 67 | + 'type' => 'service_account', |
| 68 | + 'project_id' => 'your-project-id', |
| 69 | + 'private_key_id' => 'key-id', |
| 70 | + 'private_key' => '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n', |
| 71 | + 'client_email' => 'your-service-account@your-project.iam.gserviceaccount.com', |
| 72 | + 'client_id' => '123456789', |
| 73 | + 'auth_uri' => 'https://accounts.google.com/o/oauth2/auth', |
| 74 | + 'token_uri' => 'https://oauth2.googleapis.com/token', |
| 75 | + 'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs', |
| 76 | + 'client_x509_cert_url' => 'https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com' |
| 77 | +]; |
| 78 | + |
| 79 | +// Scope definitions |
| 80 | +$scopes = [ |
| 81 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 82 | + 'https://www.googleapis.com/auth/drive.readonly' |
| 83 | +]; |
| 84 | + |
| 85 | +// Create API client |
| 86 | +$api = RestApi::createWithServiceAccount( |
| 87 | + $serviceAccountConfig, |
| 88 | + $scopes |
| 89 | +); |
| 90 | + |
| 91 | +// API calls |
| 92 | +$response = $api->request('/drive/v2/files'); |
| 93 | +``` |
| 94 | + |
| 95 | +#### Loading Service Account from JSON file |
| 96 | + |
| 97 | +```php |
| 98 | +use Keboola\Google\ClientBundle\Google\RestApi; |
| 99 | + |
| 100 | +// Load from JSON file |
| 101 | +$serviceAccountConfig = json_decode( |
| 102 | + file_get_contents('/path/to/service-account-key.json'), |
| 103 | + true |
| 104 | +); |
| 105 | + |
| 106 | +$scopes = ['https://www.googleapis.com/auth/cloud-platform']; |
| 107 | + |
| 108 | +$api = RestApi::createWithServiceAccount($serviceAccountConfig, $scopes); |
| 109 | +$response = $api->request('/your-api-endpoint'); |
| 110 | +``` |
| 111 | + |
| 112 | +## Differences between OAuth and Service Account |
| 113 | + |
| 114 | +| Property | OAuth 2.0 | Service Account | |
| 115 | +|----------|-----------|-----------------| |
| 116 | +| Authentication type | User-based | Server-to-server | |
| 117 | +| Refresh token | ✅ Yes | ❌ No (not needed) | |
| 118 | +| Authorization | Requires user consent | Automatic | |
| 119 | +| Usage | Access to user data | Access to application data | |
| 120 | +| Token expiration | Based on refresh token | Automatic renewal | |
| 121 | + |
| 122 | +## Advanced Usage |
| 123 | + |
| 124 | +### Retry and Backoff |
| 125 | + |
| 126 | +```php |
| 127 | +$api->setBackoffsCount(10); // Number of retries |
| 128 | +$api->setDelayFn(function($retries) { |
| 129 | + return 1000 * pow(2, $retries); // Exponential backoff |
| 130 | +}); |
| 131 | +``` |
| 132 | + |
| 133 | +### Logging |
| 134 | + |
| 135 | +```php |
| 136 | +use Monolog\Logger; |
| 137 | +use Monolog\Handler\StreamHandler; |
| 138 | + |
| 139 | +$logger = new Logger('google-api'); |
| 140 | +$logger->pushHandler(new StreamHandler('php://stdout')); |
| 141 | + |
| 142 | +$api = RestApi::createWithServiceAccount( |
| 143 | + $serviceAccountConfig, |
| 144 | + $scopes, |
| 145 | + $logger |
| 146 | +); |
| 147 | +``` |
| 148 | + |
| 149 | +### Custom HTTP Options |
| 150 | + |
| 151 | +```php |
| 152 | +$response = $api->request('/endpoint', 'POST', [ |
| 153 | + 'Content-Type' => 'application/json' |
| 154 | +], [ |
| 155 | + 'json' => ['key' => 'value'], |
| 156 | + 'timeout' => 30 |
| 157 | +]); |
| 158 | +``` |
| 159 | + |
| 160 | +## Testing |
| 161 | + |
| 162 | +```bash |
| 163 | +# OAuth tests (require environment variables) |
| 164 | +export CLIENT_ID="your-client-id" |
| 165 | +export CLIENT_SECRET="your-client-secret" |
| 166 | +export REFRESH_TOKEN="your-refresh-token" |
| 167 | + |
| 168 | +# Service Account tests (optional) |
| 169 | +export SERVICE_ACCOUNT_JSON='{"type":"service_account","project_id":"your-project",...}' |
| 170 | + |
| 171 | +# Run tests |
| 172 | +composer tests |
| 173 | +``` |
| 174 | + |
| 175 | +## Requirements |
| 176 | + |
| 177 | +- PHP ^7.1 |
| 178 | +- guzzlehttp/guzzle ^6.0 |
| 179 | +- google/auth ^1.26 |
4 | 180 |
|
5 | 181 | ## License |
6 | 182 |
|
7 | | -MIT licensed, see [LICENSE](./LICENSE) file. |
| 183 | +MIT |
0 commit comments