|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +/** |
| 18 | + * For instructions on how to run the full sample: |
| 19 | + * |
| 20 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/auth/README.md |
| 21 | + */ |
| 22 | + |
| 23 | +# [START auth_cloud_apikey] |
| 24 | +namespace Google\Cloud\Samples\Auth; |
| 25 | + |
| 26 | +use Google\ApiCore\ApiException; |
| 27 | +use Google\ApiCore\InsecureCredentialsWrapper; |
| 28 | +use Google\ApiCore\PagedListResponse; |
| 29 | +use Google\Cloud\Vision\V1\Client\ProductSearchClient; |
| 30 | +use Google\Cloud\Vision\V1\ListProductsRequest; |
| 31 | +use Google\Cloud\Vision\V1\Product; |
| 32 | + |
| 33 | +/** |
| 34 | + * Authenticate to a cloud API using an API key explicitly. |
| 35 | + * Note: This only works for APIs with support for API keys. |
| 36 | + * |
| 37 | + * @param string $projectId The Google Cloud project ID. |
| 38 | + * @param string $location The location name. |
| 39 | + * @param string $apiKey The API key. |
| 40 | + */ |
| 41 | +function auth_cloud_apikey(string $projectId, string $location, string $apiKey): void |
| 42 | +{ |
| 43 | + $formattedParent = ProductSearchClient::locationName($projectId, $location); |
| 44 | + |
| 45 | + // Create a client. |
| 46 | + $productSearchClient = new ProductSearchClient([ |
| 47 | + // STEP 1: Use an insecure credentials wrapper to bypass the application default credentials. |
| 48 | + 'credentials' => new InsecureCredentialsWrapper(), |
| 49 | + ]); |
| 50 | + |
| 51 | + // Prepare the request message. |
| 52 | + $request = (new ListProductsRequest()) |
| 53 | + ->setParent($formattedParent); |
| 54 | + |
| 55 | + // Call the API and handle any network failures. |
| 56 | + try { |
| 57 | + /** @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 | + ]); |
| 62 | + |
| 63 | + /** @var Product $element */ |
| 64 | + foreach ($response as $element) { |
| 65 | + printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString()); |
| 66 | + } |
| 67 | + } catch (ApiException $ex) { |
| 68 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 69 | + } |
| 70 | +} |
| 71 | +# [END auth_cloud_apikey] |
| 72 | + |
| 73 | +// The following 2 lines are only needed to run the samples |
| 74 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 75 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments