我尝试通过 GMAIL REST API v2 并使用service account key (a solution for server to server communication)从我的GMAIL
收件箱中获取最新的电子邮件。但是,我收到以下错误:
{ "error": "invalid_scope", "error_description": "Empty or missing scope not allowed." }
以下是我的PHP代码。我使用composer来安装google API PHP client。
有没有人遇到并解决了这个问题?
<?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());
}
?>