# Buggy code
def login(self):
    result = dict(changed=False)

    if self.params["client_id"] is not None:
        try:
            self.conn = pysnow.OAuthClient(
                client_id=self.client_id,
                client_secret=self.client_secret,
                token_updater=self.updater,
                instance=self.instance,
            )
        except Exception as detail:
            self.module.fail_json(
                msg="Could not connect to ServiceNow: {0}".format(str(detail)), **result
            )
    if not self.session["token"]:
        # No previous token exists, Generate new.
        try:
            self.session["token"] = self.conn.generate_token(
                self.username, self.password
            )
        except pysnow.exceptions.TokenCreateError as detail:
            self.module.fail_json(
                msg="Unable to generate a new token: {0}".format(str(detail)), **result
            )

        self.conn.set_token(self.session["token"])
    elif self.username is not None:
        try:
            self.conn = pysnow.Client(
                instance=self.instance, user=self.username, password=self.password
            )
        except Exception as detail:
            self.module.fail_json(
                msg="Could not connect to ServiceNow: {0}".format(str(detail)), **result
            )
    else:
        snow_error = "Must specify username/password or client_id/client_secret"
        self.module.fail_json(msg=snow_error, **result)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/Users/akasurde/.ansible/tmp/ansible-tmp-1563543375.7458959-114877035679906/AnsiballZ_snow_record.py", line 125, in <module>
_ansiballz_main()
File "/Users/akasurde/.ansible/tmp/ansible-tmp-1563543375.7458959-114877035679906/AnsiballZ_snow_record.py", line 117, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/Users/akasurde/.ansible/tmp/ansible-tmp-1563543375.7458959-114877035679906/AnsiballZ_snow_record.py", line 51, in invoke_module
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/ansible_snow_record_payload_brr2thzt/__main__.py", line 339, in <module>
File "/tmp/ansible_snow_record_payload_brr2thzt/__main__.py", line 335, in main
File "/tmp/ansible_snow_record_payload_brr2thzt/__main__.py", line 205, in run_module
File "/tmp/ansible_snow_record_payload_brr2thzt/ansible_snow_record_payload.zip/ansible/module_utils/service_now.py", line 57, in login
AttributeError: 'NoneType' object has no attribute 'generate_token'

# Fixed code
def login(self):
    result = dict(changed=False)

    if self.params["client_id"] is not None:
        try:
            self.conn = pysnow.OAuthClient(
                client_id=self.client_id,
                client_secret=self.client_secret,
                token_updater=self.updater,
                instance=self.instance,
            )
        except Exception as detail:
            self.module.fail_json(
                msg="Could not connect to ServiceNow: {0}".format(str(detail)), **result
            )
        if not self.session["token"]:
            # No previous token exists, Generate new.
            try:
                self.session["token"] = self.conn.generate_token(
                    self.username, self.password
                )
            except pysnow.exceptions.TokenCreateError as detail:
                self.module.fail_json(
                    msg="Unable to generate a new token: {0}".format(str(detail)),
                    **result,
                )

            self.conn.set_token(self.session["token"])
    elif self.username is not None:
        try:
            self.conn = pysnow.Client(
                instance=self.instance, user=self.username, password=self.password
            )
        except Exception as detail:
            self.module.fail_json(
                msg="Could not connect to ServiceNow: {0}".format(str(detail)), **result
            )
    else:
        snow_error = "Must specify username/password. Also client_id/client_secret if using OAuth."
        self.module.fail_json(msg=snow_error, **result)