1

我正在尝试使用 Gmail API 的节点库创建具有自定义 ID 的标签。API 有一个用于设置您自己的 id 的请求参数,但是当我尝试创建标签时,我收到错误:

{
 "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

创建标签的代码:

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();
    }
});

谢谢!

4

2 回答 2

5

您说“API 有一个用于设置您自己的 id 的请求参数”,但https://developers.google.com/gmail/api/v1/reference/users/labels/create上的文档没有显示任何此类字段users.labels.create端点的一部分。

如果您查看https://developers.google.com/gmail/api/v1/reference/users/labels,您会看到一个不可变的 id 字段,但这是不可写的,因此它的值由系统而不是你。

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


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获取此用户的完整标签列表。

于 2014-10-19T09:40:46.780 回答
0

感谢@prisoner 和gmail 的 api tester,我能够让我的 Python 版本正常工作。

我从“quickstart.py”示例开始,并克服了所有 oauth 障碍以使该代码运行。要让quickstart.py 正常工作,需要找到Rachel Hadad 的评论才能让我的凭据正常运行。Google 的文档将凭据视为一个小问题,但对我来说,这是让他们的示例运行的关键。

运行 Google 的示例代码后,我对其进行了如下所示的修改,以使用创建语法的两种变体在我的 gmail 帐户中创建名为“nood”和“foob”的新标签。花费了太多的阅读和精力来弄清楚要使用的正确语法是什么。也许这个“semiquickstart.py”会帮助像我这样发现 Google 文档几乎不透明的人。

from __future__ import print_function
import os
import os.path
import sys
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import json
import requests

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/gmail.labels']


def main():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('gmail', 'v1', credentials=creds)

    label={
     "labelListVisibility": "labelShow",
     "messageListVisibility": "show",
     "name": "nood"
    }
    
 
    results = service.users().labels().create(userId='me',body={'labelListVisibility' : 'labelShow', 'messageListVisibility' : 'show', 'name' : 'foob'}).execute()
    print(results)
    results = service.users().labels().create(userId='me',body=label).execute()
    print(results)

if __name__ == '__main__':
    main()
于 2021-10-17T23:45:10.613 回答