Gmail PHP API创建过滤器问题
问题描述:
我试图使用官方的Gmail PHP库创建一个新的标签。我使用标准的Oauth来验证用户(所有必要的权限都已经给出)。Gmail PHP API创建过滤器问题
但我收到以下错误:
<b>Fatal error</b>: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "Filter doesn't have any actions"
}
],
"code": 400,
"message": "Filter doesn't have any actions"
}
}
的代码如下:
$gmail = new Google_Service_Gmail($google);
$label = new Google_Service_Gmail_Label();
$label->setId('Label_8');
$label2 = new Google_Service_Gmail_Label();
$label2->setId('UNREAD');
$label3 = new Google_Service_Gmail_Label();
$label3->setId('INBOX');
$criteria = new Google_Service_Gmail_FilterCriteria();
$criteria->setFrom('[email protected]');
$action = new Google_Service_Gmail_FilterAction();
$action->setAddLabelIds(array($label));
$action->setRemoveLabelIds(array($label2,$label3));
$filter = new Google_Service_Gmail_Filter();
$filter->setCriteria($criteria);
$filter->setAction($action);
$result = $gmail->users_settings_filters->create('me',$filter);
作用域被设置:
$google->setScopes(array('https://www.googleapis.com/auth/pubsub','https://mail.google.com','https://www.googleapis.com/auth/gmail.settings.basic'));
一切正常程序。我甚至检查了代码并分配了这些操作。我相信图书馆可能有问题。任何建议都会有帮助。
答
通过分析日志中的问题:
$action->setAddLabelIds(array($label));
$action->setRemoveLabelIds(array($label2,$label3));
应该是:
$action->setAddLabelIds(array('Label_8'));
$action->setRemoveLabelIds(array('UNREAD','INBOX'));
的时刻不一致的API。
如果我在https://developers.google.com/gmail/api/v1/reference/users/settings/filters/create#try-it上使用它们的API浏览器执行相同的操作,它可以很好地工作。 –