我为什么会收到错误"TypeError:method()在使用Directory API的Python中只需1个参数(2个给定)?

Tyl*_*ler 3 python python-2.7 google-admin-sdk google-directory-api

我正在尝试编写一个与我们的Google Apps域中的组织单位配合使用的命令行脚本.因此,使用Google提供的许多复杂文档,我已成功在API控制台中创建了应用程序,打开了Admin SDK,并在我的脚本中成功连接.但是,当我创建目录服务对象(似乎是成功的)时,我遇到了与它交互的问题,因为我收到了该消息.我也安装了Python API包.这是我目前的代码:

import argparse
import httplib2
import os
import sys
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

f = file("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "rb")
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com",
    key,
    scope = "https://www.googleapis.com/auth/admin.directory.orgunit"
)

http = httplib2.Http()
http = credentials.authorize(http)

directoryservice = build("admin", "directory_v1", http=http)
orgunits = directoryservice.orgunits()

thelist = orgunits.list('my_customer')
Run Code Online (Sandbox Code Playgroud)

当我运行该代码时,我收到错误消息:

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    orgunits.list('my_customer')
TypeError: method() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)

我尝试没有使用"my_customer"别名,但后来错误抱怨我没有提供它.任何帮助将不胜感激,我很长一段时间没有使用Python; 它很可能是用户错误.

小智 12

我不熟悉谷歌应用程序API,但它似乎

orgunits.list()定义如下:

class FactoryObject(object):
    # ... Code Here ...

    def list(self, **kwargs):
         if 'some_parameter' not in kwargs:
             raise Exception('some_parameter required argument')
         # ... code that uses kwargs['some_parameter']
         return True
Run Code Online (Sandbox Code Playgroud)

所以如果我运行这些命令:

>>> orgunits.list()
Exception: some_parameter required argument
>>> orgunits.list('my_customer')
TypeError: list() takes exactly 1 argument (2 given)
>>> orgunits.list(some_parameter='my_customer')
True
Run Code Online (Sandbox Code Playgroud)

因此,下次您看到错误时,请尝试将参数名称添加到参数列表中,看看是否可以解决您的问题.

更多信息:

字典解包操作符(**)不像参数列表中的普通参数.如果你传递一个位置参数,当这是列表中唯一的参数时,它会抛出一个错误(就像你看到的那样),因为代码正在期待一个关键字参数.

解包运算符可以接受任意关键字参数并在字典中使用它们.

  • 面临同样的问题.通过提供参数名称和正确的参数类型来解决.blog_get_obj = blogs.get(blogId ='1233456778990066786')#Id是一个例子 (2认同)