Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an error "TypeError: method() takes exactly 1 argument (2 given) in Python using Directory API?

I am attempting to write a command line script that works with Organization Units within our Google Apps domain. Therefore, using the many convoluted documentation from Google on this, I have successfully created the application in the API Console, turned on Admin SDK, and have successfully connected within my script. However, when I create the directory service object (which seems to be successful) I am having issues interacting with it because I receive that message. I HAVE installed the Python API package as well. Here is my current code:

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

When I run that code I receive the error message:

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

I tried without using the "my_customer" alias, but then the error complains that I haven't provided it. Any help would be appreciated, I haven't use Python in a very long time; it very well may be user error.

like image 688
Tyler Avatar asked Apr 17 '26 00:04

Tyler


1 Answers

I'm not familiar with the google apps API, but it appears that

orgunits.list() is defined like:

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

So if I run these commands:

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

So next time you see the error, try adding the parameter name to your arguments list and see if that resolves your issue.

More information:

The dictionary unpack operator (**) doesn't act like a normal argument in a parameter list. If you pass a positional argument, when this is the only argument in the list, then it will throw an error (like you saw) because the code is expecting a keyword argument instead.

The unpack operator can accept arbitrary keyword arguments and use them in a dictionary.

like image 73
Jacob Gardner Avatar answered Apr 18 '26 14:04

Jacob Gardner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!