googleapis/google-cloud-php

API key

jonhassall opened this issue · 1 comments

Please improve your documentation on using an API key with your library.

Hi @jonhassall, this is a great request. The API key docs are hard to find because not every API supports them. For the APIs which ARE supported, here's how you could take advantage of them:

use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\ListKeysRequest;
use Google\ApiCore\InsecureCredentialsWrapper;

// Create a client.
$recaptcha = new RecaptchaEnterpriseServiceClient([
    // STEP 1: use insecure credentials wrapper to bypass application default creds
    'credentials' => new InsecureCredentialsWrapper(),
]);

// Prepare the request message.
$formattedParent = RecaptchaEnterpriseServiceClient::projectName('[PROJECT]');
$request = (new ListKeysRequest())->setParent($formattedParent);

// Call the API and handle any network failures.

/** @var PagedListResponse $response */
$response = $recaptchaEnterpriseServiceClient->listKeys($request, [
    // STEP 2: Pass in the API key with each RPC call as a "Call Option"
    'headers' => ['x-goog-api-key' => ['[API-KEY]']],
]);