I a trying to get the latest emails from my GMAIL
inbox via the GMAIL REST API v2 and using a service account key (a solution for server to server communication). However, I get the following error:
{ "error": "invalid_scope", "error_description": "Empty or missing scope not allowed." }
Below is my PHP code. I have used composer to install the google API PHP client.
Has anyone encountered and solved this issue?
<?php
require_once 'vendor/autoload.php';
$scopes = array('https://www.googleapis.com/auth/gmail.readonly');
$client = new Google_Client();
$client->setAuthConfig('serviceaccount-xxxxxxxxx.json');
try{
$service = new Google_Service_Gmail($client);
$opt_param['maxResults'] = 5; // Return Only 5 Messages
$opt_param['labelIds'] = 'INBOX'; // Only show messages in Inbox
$messages = $service->users_messages->listUsersMessages('me',$opt_param);
$list = $messages->getMessages();
var_dump($list);
} catch (Exception $e) {
print($e->getMessage());
}
?>