Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

node.js - Create a gmail filter with Gmail API nodejs, Error: Filter doesn't have any criteria

Im having trouble creating a filter with Gmail API using node. Auth and scopes are fine; I get err "Filter doesn't have any criteria". Im guessing that im doing something wrong with headers / body / Resource Parameter, but cant work it out.

Heres the function, I've tried a number of variants:

function createFilter(auth){
var gmail = google.gmail({version:'v1','auth':auth});
// Create filter rule
var data = {
  resource: {
    criteria: {
      from: "someone@gmail.com"
    },
    action: {
      removeLabelIds: ["INBOX"]
    }
  },
  userId: 'me'
}

// Send rule to Gmail
gmail.users.settings.filters.create(data, function (err, result) {
  if (err) {
    console.log( err);
  } else {
    console.log( result );
    callback( result );
  }
 });
};

docs:

Create Filter https://developers.google.com/gmail/api/v1/reference/users/settings/filters/create Filter Resource https://developers.google.com/gmail/api/v1/reference/users/settings/filters#resource Making Requests https://github.com/google/google-api-nodejs-client/#making-authenticated-requests

similar working req in php:

Create a filter using Gmail API

Any help would be so much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about this modification?

function createFilter(auth){
  var gmail = google.gmail('v1');
  var data = {
    "criteria": {
      "from": "someone@gmail.com",
    },
    "action": {
      "removeLabelIds": [
        "INBOX",
      ],
    },
  };
  gmail.users.settings.filters.create({
    auth: auth,
    userId: 'me',
    resource: data,
  }, function(err, result) {
    if (err) {
      console.log( err);
    } else {
      console.log( result );
      callback( result );
    }
  });
}

Note :

  • If the error of Insufficient Permission occurs, please add https://www.googleapis.com/auth/gmail.settings.basic to the scopes. When the scopes is modified, please remove the file including refresh token and then reauthorize it. By this, new scopes are reflected to the refresh token.

If this didn't work, I'm sorry.

Edit :

I confirmed that when I used v27.0.0, v26.0.1 and v25.0.0 for googleapis, the error of Filter doesn't have any criteria' occurs. So can you downgrade the version to v24.0.0? When I use v24.0.0, it works fine. It is reported that there are some bugs for the recent updated googleapis for node.js. So I'm using v24.0.0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...