简体   繁体   中英

Gmail REST API - Mark message as read

I am trying to use the Gmail REST APIs to mark a message as read.

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

This results in the following json returned:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

Has anyone else experienced this? I'm at a loss as to what may be causing this.

I was able to figure this out and get it working. The only difference to be made was to stringify the data like so:

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

Making this change made it work.

Try adding ContentType = "application/json; charset=UTF-8" in the code. Also, check this link for the detail information on the error.

Let me know if you still see the error.

I had the same problem doing this in Meteor. The problem was I was passing the removeLableIds in the 'params' attribute. Changing to the 'data' attribute worked, ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM