1

I am receiving this error message, when I try to read a spreadsheet from my google drive:

 "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }

This is my code:

$json = [
    'projectId' => $this->generalHelper->getProjectId(),
    'keyFile' => json_decode('{
          "type": "' . $this->generalHelper->getServiceType() . '",
          "private_key_id": "' . $this->generalHelper->getPrivateKeyId() . '",
          "private_key": "' . $this->generalHelper->getPrivateKey() . '",
          "client_email": "' . $this->generalHelper->getClientEmail() . '",
          "client_id": "' . $this->generalHelper->getClientId() . '",
          "auth_uri": "' . $this->generalHelper->getAuthUri() . '",
          "token_uri": "' . $this->generalHelper->getTokenUri() . '",
          "auth_provider_x509_cert_url": "' . $this->generalHelper->getAuthProvider() . '",
          "client_x509_cert_url": "' . $this->generalHelper->getCertUrl() . '"
    }', true)
];

$client = new Google_Client($json);
$service = new Google_Service_Sheets($client);
try {
    $response = $service->spreadsheets->get($this->generalHelper->getSpreadSheetId());
}catch (Exception $error){
    var_dump($error->getMessage()); exit();
}

The $json variable contains the content of that json file, from the Service Accounts-> key. My key is active and it has the owner role. Overall, it looks good. Not sure what am I missing here. Please advice :) Thank you

enter image description here

Side note: I followed this tutorial : https://www.srijan.net/resources/blog/integrating-google-sheets-with-php-is-this-easy-know-how

Attila Naghi
  • 2,535
  • 6
  • 37
  • 59
  • Consider using the officially supported Google Client Library to connect to the Sheets API from PHP [quickstart here](https://developers.google.com/sheets/api/quickstart/php) – Rafa Guillermo Jul 12 '21 at 12:56
  • @RafaGuillermo I am using it :) I am using their libraries. Do you have any idea what should I check , I am sure it must be some configuration that I am missing. – Attila Naghi Jul 12 '21 at 13:53
  • I would suggest reading [this](https://cloud.google.com/iam/docs/service-accounts) and [this](https://stackoverflow.com/questions/61744176/creating-google-calendar-events-with-a-gcp-service-account/61778053#61778053) for more information on how service accounts work – Rafa Guillermo Jul 14 '21 at 14:11

0 Answers0