【问题标题】:Gmail REST API - Mark message as readGmail REST API - 将邮件标记为已读
【发布时间】:2015-01-15 20:48:42
【问题描述】:

我正在尝试使用 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"
 }
}

有其他人经历过吗?我不知道是什么原因造成的。

【问题讨论】:

  • 为什么标题上写着Mark message as read,而数据里有"addLabelIds": ["UNDREAD"]

标签: javascript jquery rest gmail-api


【解决方案1】:

我能够解决这个问题并让它发挥作用。唯一的区别是像这样对数据进行字符串化:

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

进行此更改使其生效。

【讨论】:

    【解决方案2】:

    尝试在代码中添加 ContentType = "application/json; charset=UTF-8"。另外,请查看此link 以获取有关错误的详细信息。

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2011-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 2017-12-19
        • 2014-10-26
        • 1970-01-01
        • 2011-01-12
        相关资源
        最近更新 更多