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 ]
2424namespace Google \Cloud \Samples \Auth ;
2525
2626use Google \ApiCore \ApiException ;
27- use Google \ApiCore \InsecureCredentialsWrapper ;
2827use Google \ApiCore \PagedListResponse ;
2928use Google \Cloud \Vision \V1 \Client \ProductSearchClient ;
3029use 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
7469require_once __DIR__ . '/../../testing/sample_helpers.php ' ;
0 commit comments