I'm trying to fetch the gmail inbox directly from backend by a cronjob, with no browser or oauth thing.
here is the error i get
Google\Service\Exception: {
"error": {
"code": 400,
"message": "Precondition check failed.",
"errors": [
{
"message": "Precondition check failed.",
"domain": "global",
"reason": "failedPrecondition"
}
],
"status": "FAILED_PRECONDITION"
}
}
and here is my code
$client = new Google_Client();
$client->setAuthConfig(base_path('config/keys/ticketing-system-401805-20cdedb73268.json'));
$gmailConfig = config('services.gmail');
$client->setClientId($gmailConfig['client_id']);
$client->setClientSecret($gmailConfig['client_secret']);
$client->setScopes($gmailConfig['scopes']);
// Create a Gmail service using the service account client
$service = new Google_Service_Gmail($client);
// List the user's Gmail messages
$messages = $service->users_messages->listUsersMessages('me', []);
foreach ($messages->getMessages() as $message)
{
// Retrieve and process each email
$email = $service->users_messages->get('me', $message->getId());
// You can access the email content with $email->getBody() and other properties.
}
in config/services.php i got this added
'gmail' => [
'client_id' => 'CLIENT_ID',
'client_secret' => 'CLIENT_SECRET',
'project_id' => 'ticketing-system-401805',
// ...
'scopes' => [
'https://www.googleapis.com/auth/gmail.readonly',
// Add other required scopes as needed
],
'key_file' => base_path('config/keys/ticketing-system-401805-20cdedb73268.json'),
],
I found the client_id easily, but the client_secret i didnt find one in service accounts or api key, just a one into the oauth2 client_id, so i just copied & paste, which i feel is wrong because iam not using oauth to validate the process, but i got nothing else to put there
any ideas to how to solve this, or even a proper documentation for the process ?