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

时间:2014-11-18 15:40:29

标签: javascript jquery rest gmail-api

我正在尝试使用Gmail REST API将邮件标记为已读。

$('#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"
 }
}

还有其他人经历过这个吗?我对可能造成这种情况的原因感到茫然。

3 个答案:

答案 0 :(得分:2)

我能够弄明白这一点并让它发挥作用。唯一的区别是像这样对数据进行字符串化:

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

进行此更改使其有效。

答案 1 :(得分:1)

尝试添加ContentType =&#34; application / json;字符集= UTF-8&#34;在代码中。另外,请检查此link以获取有关错误的详细信息。

如果您仍然看到错误,请告诉我。

答案 2 :(得分:1)

我在Meteor中遇到同样的问题。问题是我在'params'属性中传递了removeLableIds。更改为“数据”属性,即

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 
        }
});

相关问题