Skip to content

Commit c8129ac

Browse files
authored
feat: update apikey sample for client option support (#2061)
1 parent 6085ee3 commit c8129ac

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

auth/src/auth_cloud_apikey.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/auth/README.md
2121
*/
2222

23-
# [START auth_cloud_apikey]
23+
# [START apikeys_authenticate_api_key]
2424
namespace Google\Cloud\Samples\Auth;
2525

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

4544
// Create a client.
4645
$productSearchClient = new ProductSearchClient([
47-
// STEP 1: Use an insecure credentials wrapper to bypass the application default credentials.
48-
'credentials' => new InsecureCredentialsWrapper(),
46+
'apiKey' => $apiKey,
4947
]);
5048

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

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

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

0 commit comments

Comments
 (0)