Gmail REST API - 将邮件标记为已读

3

我正在尝试使用Gmail REST APIs将一条消息标记为已读。

$('#markGmailRead').click(function(){
    var request = $.ajax({
        type: 'POST',
        dataType: 'json',
        headers: { "Authorization": "Bearer <<ACCESS KEY>>",
                    "Content-Type": "application/json"},
        url: 'https://www.googleapis.com/gmail/v1/users/me/messages/<<MESSAGEID>>/modify',
        data: {"addLabelIds": ["UNREAD"]}
    })
    request.done(function(data){
        // when the Deferred is resolved.
        console.log(data);
    })
    request.fail(function(){
        // when the Deferred is rejected.
        console.log('fail');
    })
})

这会导致返回以下JSON:
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

有其他人也遇到过这个问题吗?我不知道是什么原因导致的。


为什么标题说“将消息标记为已读”,而数据包含“addLabelIds”:[“UNDREAD”]? - Erik Philips
3个回答

2

我成功地解决了这个问题并让它正常工作。唯一需要改变的是将数据字符串化,像这样:

data: JSON.stringify({"removeLabelIds":["UNREAD"]}),

修改后使其正常工作。


1
尝试在代码中添加ContentType = "application/json; charset=UTF-8"。此外,查看此链接以获取有关错误的详细信息。 如果您仍然看到错误,请告诉我。

1

我在使用Meteor时遇到了同样的问题。问题在于我将removeLableIds作为“params”属性传递。改为使用“data”属性即可解决,例如:

var apiUrl = "https://www.googleapis.com/gmail/v1/users/me/messages/" 
     + messageID + "/modify";

var result = HTTP.post( apiUrl, {
        data: { "removeLabelIds": ["INBOX"] },
        headers:{
              "content-type":"application/json ; charset=UTF-8", 
              "Authorization": "Bearer " + tokens.accessToken 
        }
});


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接