Every request I make to users.messages.modify (https://developers.google.com/gmail/api/v1/reference/users/messages/modify) returns:
{ errors:
[ { domain: 'global',
reason: 'invalidArgument',
message: 'No label add or removes specified' } ],
code: 400,
message: 'No label add or removes specified' }
I am making the request like:
var service = Google.gmail({version : 'v1', auth : oauth2Client});
service.users.messages.modify({
'userId': 'some email address',
'id': 'some message id',
'addLabelIds': ['some label id'],
'removeLabelIds': []
}, function (err) {
if (!err) {
callback(true);
} else {
console.log(err);
callback(false);
}
});
Not sure if I am just missing something or if this is an issue with the library.
I suspect you only need to specify removeLabelIds if there are any labels to be removed - try to omit this request parameter, see if that works...
Also, are you supplying the label's ID and not the label's display name?
I have tried only supplying the addLabelIds parameter and it didn't work either and yes the 'some label id' is the label ID return from users.labels.list. I posted a question on Stack Overflow (http://stackoverflow.com/questions/26492633/adding-label-to-message-via-google-gmail-api) about this issue as well that has some more information.
I solved it. The problem is you have to pass the addLabelIds parameter inside a resource object:
var service = Google.gmail({version : 'v1', auth : oauth2Client});
service.users.messages.modify({
'userId': 'some email address',
'id': 'some message id',
'resouce':{
'addLabelIds': ['some label id'],
'removeLabelIds': []
}
}, function (err) {
if (!err) {
callback(true);
} else {
console.log(err);
callback(false);
}
});
The docs only say "In the request body, supply data with the following structure:"
{
"addLabelIds": [
string
],
"removeLabelIds": [
string
]
}
They don't say anything about a resource object. Same goes for the users.labels.create function it says to put the request body in a label object but this library actually only takes a resource object.
Ah, the resource object in the parameters is the request body. All other parameters are either query parameters (appended to the url) or path parameters (in the url itself). Seems this is an issue related to misinterpreting the terminology used in this library and the API docs. Sorry about that! Closing because you got it figured it out but thanks for the feedback!
@ewein thanks for the tip. saved me some time.
It's incredible.
The google api docs example specifies this:
function modifyMessage(userId, messageId, labelsToAdd, labelsToRemove, callback) {
var request = gapi.client.gmail.users.messages.modify({
'userId': userId,
'id': messageId,
'addLabelIds': labelsToAdd,
'removeLabelIds': labelsToRemove
});
request.execute(callback);
}
Take a look to the javascript example at https://developers.google.com/gmail/api/v1/reference/users/messages/modify
I was doing it that way, and I was taking this error:
< { [Error: No label add or removes specified]
< code: 400,
< errors:
< [ { domain: 'global',
< reason: 'invalidArgument',
< message: 'No label add or removes specified' } ] }
The solution found here worked great.
thank you.
Doc still hasn't been updated :(
Spent time trying to figure out what was wrong before I came across this.
Hi, guys! Writing to You from Feb 2019 (so ~4 years 3 months passed since the question has been asked here) and want to confirm that they still didn't fix the docs, API returns the same error and the solution @ewein provided works perfectly!
Long live @ewein ps we miss you at btag buddy =]
I got that error also, even the solution of @ewein didn't work for me, so i have digged in the implimentation of the function and i found that the structure of the params argument looks like this:
interface Params$Resource$Users$Messages$Modify extends StandardParameters {
/**
* Auth client or API Key for the request
*/
auth?: string | OAuth2Client | JWT | Compute | UserRefreshClient;
/**
* The ID of the message to modify.
*/
id?: string;
/**
* The user's email address. The special value me can be used to indicate the authenticated user.
*/
userId?: string;
/**
* Request body metadata
*/
requestBody?: Schema$ModifyMessageRequest;
}
with Schema$ModifyMessageRequest looks like this:
interface Schema$ModifyMessageRequest {
/**
* A list of IDs of labels to add to this message.
*/
addLabelIds?: string[];
/**
* A list IDs of labels to remove from this message.
*/
removeLabelIds?: string[];
}
so the right way to write the params is:
function modifyMessage(userId, messageId, labelsToAdd, labelsToRemove, callback) {
var request = gapi.client.gmail.users.messages.modify({
'userId': userId,
'id': messageId,
'requestBody': {
'addLabelIds': labelsToAdd,
'removeLabelIds': labelsToRemove
}
});
request.execute(callback);
}
hope that help you guys, and they update the docs.
Most helpful comment
I solved it. The problem is you have to pass the addLabelIds parameter inside a resource object:
The docs only say "In the request body, supply data with the following structure:"
They don't say anything about a resource object. Same goes for the users.labels.create function it says to put the request body in a label object but this library actually only takes a resource object.