Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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