Gmail PHP API 创建过滤器问题

Gmail PHP API Create Filter Issue

我正在尝试使用官方 Gmail PHP 库创建新标签。我正在使用标准 Oauth 来验证用户(已提供所有必要的权限)。

但我收到以下错误:

<b>Fatal error</b>:  Uncaught exception 'Google_Service_Exception' with message '{
 &quot;error&quot;: {
  &quot;errors&quot;: [
   {
    &quot;domain&quot;: &quot;global&quot;,
    &quot;reason&quot;: &quot;invalidArgument&quot;,
    &quot;message&quot;: &quot;Filter doesn't have any actions&quot;
   }
  ],
  &quot;code&quot;: 400,
  &quot;message&quot;: &quot;Filter doesn't have any actions&quot;
 }
}

代码如下:

$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('test@gmail.com');

      $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。