Search code examples
phpcodeignitergoogle-apigoogle-cloud-platformgoogle-cloud-vision

Code 403 : "The request is missing a valid API key." PERMISSION_DENIED Service Account Key Google Cloud Vision PHP


I want to implement the Google Cloud Vision with ImageAnnotator using a service key. What i have try is like below :

Error :

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

When try this code :

defined('BASEPATH') OR exit('No direct script access allowed');

use Google\Cloud\Vision\VisionClient;

class Admin_center extends CI_Controller {
    function __construct() {
        parent::__construct();

        include APPPATH . 'third_party/vendor/autoload.php';
    }

    public function index() {
        $this->load->view('index');
    }

    function upload_ocr_image() {               
        $img_data = $this->upload->data();                                          

        $vision = new VisionClient(['keyfile' => json_decode(file_get_contents(base_url().'assets/google_cloud_vision/credentials.json'), true)]);

        $imageRes = fopen($img_data['full_path'], 'r');
        $image = $vision->image($imageRes,['Text_Detection']);
        $result = $vision->annotate($image);

        print_r($result);
    }
}

I used a service account key.

Why i got error : 403 Permissin Denied and Missing a valid API Key ?

Edited :

I have follow this youtube tutorial :

https://www.youtube.com/watch?v=K-tpjOT7k-o

https://www.youtube.com/watch?v=PqAXE67fwu8&t=2s

Thank You


Solution

  • Finally IT WORKED after 5 hours struggle to find the problem.

    Actually the code i have posted above it is worked. I just mistype the Array name.

    So this line :

    $vision = new VisionClient(['keyfile' => json_decode(file_get_contents(base_url().'assets/google_cloud_vision/credentials.json'), true)])
    

    the 'keyfile' should be 'keyFile' with 'F' is an uppercase.

    That's all.

    Thank you for all you helps..