ディレクトリAPIを使用して、Pythonでエラー「TypeRror:Method()がPythonで正確に1つの引数(2与えられた)を取得するのはなぜですか?

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

質問

Google Appsドメイン内の組織ユニットで動作するコマンドラインスクリプトを作成しようとしています。したがって、これに関するGoogleからの多くの複雑なドキュメントを使用して、APIコンソールでアプリケーションを正常に作成し、管理者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