2017-04-10 59 views
0

我有下面的代碼來觀看郵箱。正如所見,在lableIds中,我只有一個標籤,INBOX,因爲我只想聽新消息。但是,當我運行它時,它會每隔30秒左右收到不同messageID的通知。然而,在INBOX中沒有發生任何變化,沒有添加/刪除新項目。如何將我的手錶機身設置爲僅收聽收到的消息的INBOX?Gmail API:觀看收件箱標籤只有

certificate= new X509Certificate2("file.p12"), "password",  
X509KeyStorageFlags.Exportable); 
credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceaccount) 
        { 
         User = username,//username being impersonated 
         Scopes = scopes 
        }.FromCertificate(certificate)); 
var service = new GmailService(new BaseClientService.Initializer() 
    { 
     HttpClientInitializer = credential, 
     ApplicationName = applicationame, 
    }); 


WatchRequest body = new WatchRequest() 
{ 
    TopicName = "projects/projectid/topics/topicname", 
    LabelIds = new[] {"INBOX"} 
} 
string userId = "me"; 
UsersResource.WatchRequest watchRequest = service.Users.Watch(body, userId); 
WatchResponse test = watchRequest.Execute(); 
+0

對我而言,您使用的是服務帳戶。您是否按照Google Identity Platform的官方文檔https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority中的說明模仿用戶? – Morfinismo

+0

@Morfinismo我編輯了代碼,以突出顯示我在被模擬的用戶的ID中傳遞的位置。我確實授予了我的服務帳戶在全域範圍內的權限。 – jpo

回答

1

Gmail顯然忽略了請求的過濾器數據。這看起來是gmail中的一個已知的bug。結帳https://issuetracker.google.com/issues/37300052來監控進度,雖然它似乎很安靜。我建議填寫錯誤報告,至少重複一遍。

對於我的用例,我只查找具有特定自定義標籤的電子郵件。可以存儲該用戶的歷史ID(請參閱https://developers.google.com/gmail/api/v1/reference/users/history/list),因爲這是一個時代,所以每隔X分鐘左右就只能調用一次gmail API。這會減少您對Gmail所做的調用次數,但並不完美。

相關問題