通过Google Gmail API创建带有ID的标签 [英] Creating label with id via Google Gmail API

查看:126
本文介绍了通过Google Gmail API创建带有ID的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用Gmail API的节点库为自定义ID创建标签。 API有一个请求参数来设置你自己的ID,但是当我尝试创建标签时,我得到了错误:

I am trying to create a label with a custom id using the node library for the Gmail API. The API has a request parameter for setting your own id however when I try to create the label I get the error:

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

当我没有给出id时,创建标签时没有问题。然而,为了我的目的,我需要有一套标准的标签ID。任何人都知道这里发生了什么,或者它只是api的错误/错误?
您可以尝试为您的帐户创建自己的标签,并在此处查看更多我在说的内容: https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.labels.create

The label is created with no problems when I don't give an id. However for my purposes I need to have a set standard label id. Anyone know what is going on here or if it is just a bug/error with the api? You can try creating your own label for your account and see more of what I am talking about here: https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.labels.create

创建标签的代码:

Code to create label:

var service = Google.gmail({version : 'v1', auth : oauth2Client});
service.users.labels.create({
    userId : 'user address here',
    labelListVisibility   : 'labelShow',
    messageListVisibility : 'show',
    name : 'label name here',
    id   : 'label id here'
}, function (err) {
    if (err) {
        throw err;
    } else {
       callback();
    }
});

谢谢!

Thanks!

推荐答案

你说API有一个设置你自己的id的请求参数,但是 https://developers.google.com/gmail/api/v1/reference/users/labels/create 不会显示任何此类字段作为 users.labels.create 端点。

You say "The API has a request parameter for setting your own id", but the documentation at https://developers.google.com/gmail/api/v1/reference/users/labels/create does not show any such field as part of the users.labels.create endpoint.

如果您查看,你会看到一个不可变的id字段,但这是不可写的,所以这个值由系统而不是你设置。

If you look at https://developers.google.com/gmail/api/v1/reference/users/labels, you'll see an immutable id field, but this is not writable, so the value for this is set by the system rather than by you.

users.labels.create 还表示将返回完全填充的用户对象,因此您将能够知道刚刚创建的标签的ID。要使用node.js库执行此操作,请将回调函数设置为具有第二个参数,该参数将包含调用的结果。所以它可能看起来像这样:

The documentation for users.labels.create also indicates that a fully populated Users object will be returned, so you'll be able to know what the ID for the label you just created was. To do this with the node.js library, you set the callback function to have a second parameter, which will contain the results of the call. So it might look something like this:


var service = Google.gmail({version : 'v1', auth : oauth2Client});
service.users.labels.create({
    userId : 'user address here',
    labelListVisibility   : 'labelShow',
    messageListVisibility : 'show',
    name : 'label name here'
}, function (err, result) {
    if (err) {
        throw err;
    } else {
       console.log( result );
       callback( result );
    }
});

正如评论中指出的那样,您还可以使用 users.labels.list 获取此用户的完整标签列表。

As noted in the comments, you can also use users.labels.list to get a full list of labels for this user.

这篇关于通过Google Gmail API创建带有ID的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆