googleapis / google-cloud-php

Google Cloud Client Library for PHP
https://cloud.google.com/php/docs/reference
Apache License 2.0
1.09k stars 433 forks source link

API key #7347

Open jonhassall opened 3 months ago

jonhassall commented 3 months ago

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

bshaffer commented 2 months ago

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]']],
]);