20
20
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/auth/README.md
21
21
*/
22
22
23
- # [START auth_cloud_apikey ]
23
+ # [START apikeys_authenticate_api_key ]
24
24
namespace Google \Cloud \Samples \Auth ;
25
25
26
26
use Google \ApiCore \ApiException ;
27
- use Google \ApiCore \InsecureCredentialsWrapper ;
28
27
use Google \ApiCore \PagedListResponse ;
29
28
use Google \Cloud \Vision \V1 \Client \ProductSearchClient ;
30
29
use Google \Cloud \Vision \V1 \ListProductsRequest ;
@@ -44,8 +43,7 @@ function auth_cloud_apikey(string $projectId, string $location, string $apiKey):
44
43
45
44
// Create a client.
46
45
$ productSearchClient = new ProductSearchClient ([
47
- // STEP 1: Use an insecure credentials wrapper to bypass the application default credentials.
48
- 'credentials ' => new InsecureCredentialsWrapper (),
46
+ 'apiKey ' => $ apiKey ,
49
47
]);
50
48
51
49
// Prepare the request message.
@@ -55,10 +53,7 @@ function auth_cloud_apikey(string $projectId, string $location, string $apiKey):
55
53
// Call the API and handle any network failures.
56
54
try {
57
55
/** @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 );
62
57
63
58
/** @var Product $element */
64
59
foreach ($ response as $ element ) {
@@ -68,7 +63,7 @@ function auth_cloud_apikey(string $projectId, string $location, string $apiKey):
68
63
printf ('Call failed with message: %s ' . PHP_EOL , $ ex ->getMessage ());
69
64
}
70
65
}
71
- # [END auth_cloud_apikey ]
66
+ # [END apikeys_authenticate_api_key ]
72
67
73
68
// The following 2 lines are only needed to run the samples
74
69
require_once __DIR__ . '/../../testing/sample_helpers.php ' ;
0 commit comments