Search code examples
javascriptgmail-api

gapi.client.gmail.users.labels.create gives invalidArgument error


I am working on creating labels using JavaScript Gmail API.

function createLabel(userId, newLabelName, callback) {
var request = gapi.client.gmail.users.labels.create({
'userId': userId,
'label': {'name': newLabelName}
});
request.execute(callback);}

This is the code of Developer API Example

I am able to authenticate and even I'll get the label list but while creating the label I am getting the below error.

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidArgument",
    "message": "Invalid request"
   }
  ],
  "code": 400,
  "message": "Invalid request"
 }
}

But when I am using the Try It example It's working but only difference is it's do the POST request with key={Your auth key} which the example provided won't attach that part to URL.


Solution

  • The data you send should be in the resource-parameter, and labelListVisibility and messageListVisibility are also mandatory fields:

    function createLabel(userId, newLabelName, callback) {
      gapi.client.gmail.users.labels.create({
        userId: userId,
        resource: {
          name: newLabelName,
          labelListVisibility: 'labelShow',
          messageListVisibility: 'show'
        }
      }).execute(callback);
    }