为什么我会使用Directory API在Python中恰好在Python中遇到错误“ typeerror:type(Method())?

StackOverflow https://stackoverflow.com/questions/19842347

我正在尝试编写一个命令行脚本,该脚本与我们的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')

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

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

我尝试不使用“ my_customer”别名,但随后错误抱怨说我还没有提供。任何帮助都将不胜感激,我很久没有使用Python。很可能是用户错误。

有帮助吗?

解决方案

我不熟悉Google Apps 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

因此,如果我运行以下命令:

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

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

更多信息:

字典拆箱操作员(**)的作用不像参数列表中的普通参数。如果您通过位置参数,那么当这是列表中唯一的参数时,它将引发错误(如您所见),因为代码正在期待关键字参数。

解开操作员可以接受任意关键字参数,并在字典中使用它们。

其他提示

可以是Python过去了吗 self 自动地?我也是Python的新手,所以我不确定Python何时这样做,但过去对我造成了一些困惑。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top