0

I am trying to create a new user with the Azure Active Directory libraries for Python, authenticating the user with the UserPassCredentials Class throws a positional argument error. The UserPassCredentials class. All the parameters are defined:

 credentials = UserPassCredentials(username, password, client_id, secret, resource)

This is the error:

TypeError: __init__() takes from 3 to 5 positional arguments but 6 were given

There are exactly 5 arguments. Why do I keep getting this error?

I understand the Azure AD Graph API will have some features deprecated, and it is recommended to use Microsoft Graph API. I just need some help understanding why it returns this error.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
ghenzi83
  • 109
  • 1
  • 13

3 Answers3

0

First of all, In previous version of the SDK, ADAL was not yet available and we provided a UserPassCredentials class. This is considered deprecated and should not be used anymore. This does not support 2FA. But as per my earlier experience with this class, we either pass below combination while creating credential object

1) User name, password, 2) User Name, Password, resource 3) Client Id , secret 4) client Id , secret and resource

Resource is by default ('https://management.core.windows.net/'.)

something like this:

return UserPassCredentials(

        config_data["username"],

        config_data["password"],

    ) 

UserPassCredentials(username, password, client_id=None, secret=None, **kwargs)

last param is option which can have following values

Optional kwargs may include:

cloud_environment (msrestazure.azure_cloud.Cloud): A targeted cloud environment

china (bool): Configure auth for China-based service, default is 'False'.

tenant (str): Alternative tenant, default is 'common'.

resource (str): Alternative authentication resource, default is 'https://management.core.windows.net/'.

verify (bool): Verify secure connection, default is 'True'.

timeout (int): Timeout of the request in seconds.

proxies (dict): Dictionary mapping protocol or protocol and hostname to the URL of the proxy.

cache (adal.TokenCache): A adal.TokenCache, see ADAL configuration

I am assuming , you are passing right values in the param, Also please try to pass the combination i mentioned above and see if it works.

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • Thanks for the suggestion. I tried this but it but got an error 'got multiple values for argument client_id' - Mohit Verma - MSFT – ghenzi83 Mar 19 '19 at 16:08
0

First, to answer your Python question about:

TypeError: __init__() takes from 3 to 5 positional arguments but 6 were given

Look at the documentation of this class, the signature is:

UserPassCredentials(username, password, client_id=None, secret=None, **kwargs)

Using positional syntax, you need at least 2, and at most 4 (+ self which always count for one), so the message is right "from 3 to 5". When you pass "resource" in your example, you are passing a 6th positional argument, that does not respect the Python signature (again, self counts for one!). This has nothing to do with Azure or SDK, this is pure Python :)

Now, to solve your particular problem, GraphRBAC API requires a resource parameter to always be https://graph.windows.net. You can't change that. So the minimal construction will be:

credentials = UserPassCredentials(
        'user@domain.com',      # Your user
        'my_password',          # Your password
        resource="https://graph.windows.net"
)

This doc might help: https://learn.microsoft.com/python/api/overview/azure/activedirectory

That's usually enough to create the client. If you have more questions, don't hesitate to open an issue on Github: https://github.com/Azure/azure-sdk-for-python/issues

(I work at MS in the Azure SDK for Python team, and own this code actually :))

Laurent Mazuel
  • 3,422
  • 13
  • 27
  • 1
    This option returns 'AdalError: he request body must contain the following parameter: 'client_assertion' or 'client_secret'. Thanks for your help. I got it working with the MS Graph API! – ghenzi83 Mar 19 '19 at 16:10
0

I decided to use the MS Graph API. Using requests and adal libraries got this working for me

ghenzi83
  • 109
  • 1
  • 13