2017-02-21 52 views
0

我試圖使用官方的Gmail PHP庫創建一個新的標籤。我使用標準的Oauth來驗證用戶(所有必要的權限都已經給出)。Gmail PHP API創建過濾器問題

但我收到以下錯誤:

<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('[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')); 

一切正常程序。我甚至檢查了代碼並分配了這些操作。我相信圖書館可能有問題。任何建議都會有幫助。

+0

如果我在https://developers.google.com/gmail/api/v1/reference/users/settings/filters/create#try-it上使用它們的API瀏覽器執行相同的操作,它可以很好地工作。 –

回答

0

通過分析日誌中的問題:

$action->setAddLabelIds(array($label)); 
    $action->setRemoveLabelIds(array($label2,$label3)); 

應該是:

$action->setAddLabelIds(array('Label_8')); 
    $action->setRemoveLabelIds(array('UNREAD','INBOX')); 

的時刻不一致的API。