Gmail REST API - Mark a message as read

I am trying to use the Gmail REST API 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'); }) }) 

As a result, the following json is returned:

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

Has anyone else experienced this? I do not understand what could be causing this.

+3
source share
3 answers

I was able to figure this out and make it work. The only difference was to flatten the data like this:

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

Making this change made it work.

+2
source

Try adding ContentType = "application / json; charset = UTF-8" to the code. Also check this link for detailed error information.

Let me know if you still see the error.

+1
source

I had the same problem as in Meteor. The problem was that I was passing removeLableIds in the params attribute. Transition to the attribute "data", i.e.

 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 } }); 
+1
source

All Articles