Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
like image 839
justberare Avatar asked Oct 12 '25 04:10

justberare


1 Answers

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.

like image 172
Dan D. Avatar answered Oct 14 '25 21:10

Dan D.