Skip to content

Commit

Permalink
feat: update apikey sample for client option support (#2061)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Nov 21, 2024
1 parent 6085ee3 commit c8129ac
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions auth/src/auth_cloud_apikey.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/auth/README.md
*/

# [START auth_cloud_apikey]
# [START apikeys_authenticate_api_key]
namespace Google\Cloud\Samples\Auth;

use Google\ApiCore\ApiException;
use Google\ApiCore\InsecureCredentialsWrapper;
use Google\ApiCore\PagedListResponse;
use Google\Cloud\Vision\V1\Client\ProductSearchClient;
use Google\Cloud\Vision\V1\ListProductsRequest;
Expand All @@ -44,8 +43,7 @@ function auth_cloud_apikey(string $projectId, string $location, string $apiKey):

// Create a client.
$productSearchClient = new ProductSearchClient([
// STEP 1: Use an insecure credentials wrapper to bypass the application default credentials.
'credentials' => new InsecureCredentialsWrapper(),
'apiKey' => $apiKey,
]);

// Prepare the request message.
Expand All @@ -55,10 +53,7 @@ function auth_cloud_apikey(string $projectId, string $location, string $apiKey):
// Call the API and handle any network failures.
try {
/** @var PagedListResponse $response */
$response = $productSearchClient->listProducts($request, [
// STEP 2: Pass in the API key with each RPC call as a "Call Option"
'headers' => ['x-goog-api-key' => [$apiKey]],
]);
$response = $productSearchClient->listProducts($request);

/** @var Product $element */
foreach ($response as $element) {
Expand All @@ -68,7 +63,7 @@ function auth_cloud_apikey(string $projectId, string $location, string $apiKey):
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
}
}
# [END auth_cloud_apikey]
# [END apikeys_authenticate_api_key]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
Expand Down

0 comments on commit c8129ac

Please sign in to comment.