【发布时间】:2014-10-06 17:15:05
【问题描述】:
当使用以下代码尝试列出我刚刚设置的全新 gmail 帐户收到的邮件时,我得到了
[Google_Service_Exception] Error calling GET https://www.googleapis.com/gmail/v1/users/myGmailAccount%40gmail.com/messages: (500) Backend Error
我知道验证部分工作正常,因为我可以进行细微的修改并查询书籍 api,如 this example 所示。下面是我用来尝试查询最近收到的消息的代码...
const EMAIL = 'myGmailAccount@gmail.com';
private $permissions = [
'https://www.googleapis.com/auth/gmail.readonly'
];
private $serviceAccount = 'xxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
/** @var Google_Service_Gmail */
private $gmail = null;
/** @var null|string */
private static $serviceToken = null;
public function __construct()
{
$client = new Google_Client();
$client->setApplicationName($this->applicationName);
$this->gmail = new Google_Service_Gmail($client);
//authentication
if (isset(self::$serviceToken)) {
$client->setAccessToken(self::$serviceToken);
}
$credentials = new Google_Auth_AssertionCredentials(
$this->serviceAccount,
$this->permissions,
$this->getKey()
);
$client->setAssertionCredentials($credentials);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($credentials);
}
self::$serviceToken = $client->getAccessToken();
}
public function getMessages()
{
$this->gmail->users_messages->listUsersMessages(self::EMAIL);
}
我已授予对 gmail 的 API 访问权限:
500 让我相信这是 gmail API 的内部错误,或者我遗漏了 PHP 客户端未验证的内容。
【问题讨论】:
标签: php google-api-php-client gmail-api