简体   繁体   中英

Where to put API Key in Google Cloud Vision for PHP

I want to use Google Cloud Vision API on a family photo. I activated the API on my GCP account, received an API Key but I don't know where I should insert it. Here's my code :

<?
require 'vendor/autoload.php';
use Google\Cloud\Vision\VisionClient;
$vision = new VisionClient();
$image = $vision->image(
    fopen('data/family_photo.jpg', 'r'),
    ['faces']
);
$annotation = $vision->annotate($image);
var_dump($annotation);
die();

?>

I get the following error : "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }.

Update: Thanks to the provided answer by Dan D., I added the following line:

putenv("GOOGLE_APPLICATION_CREDENTIALS=book.json");

I think that for using the Google Cloud PHP SDK you need to use a Service Account. As stated here ( https://github.com/googleapis/google-cloud-php/blob/master/AUTHENTICATION.md ), you will need to have an environment variable setup that points to the JSON file with the credentials.

$ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

Also you can follow this guide ( https://cloud.google.com/video-intelligence/docs/common/auth ) to create Service Account and generate the JSON file. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM