# Buggy code
def _get_app_object_id_from_sp_object_id(client, sp_object_id):
    sp = client.service_principals.get(sp_object_id)
    app_object_id = None

    if sp.service_principal_names:
        result = list(
            client.applications.list(
                filter="identifierUris/any(s:s eq '{}')".format(
                    sp.service_principal_names[0]
                )
            )
        )
        if result:
            app_object_id = result[0].object_id
    return app_object_id


# Traceback (msrest.exceptions.ValidationError)
$ az ad sp credential list --id yyy-zzz-zzzz-zzzz-xxx
The command failed with an unexpected error. Here is the traceback:

Parameter 'application_object_id' can not be None.
Traceback (most recent call last):
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/knack/cli.py", line 206, in invoke
cmd_result = self.invocation.execute(args)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 351, in execute
raise ex
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 409, in _run_jobs_serially
results.append(self._run_job(expanded_arg, cmd_copy))
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 400, in _run_job
cmd_copy.exception_handler(ex)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/command_modules/role/commands.py", line 69, in graph_err_handler
raise ex
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 379, in _run_job
result = cmd_copy(params)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 171, in __call__
return self.handler(*args, **kwargs)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/core/__init__.py", line 451, in default_command_handler
return op(**command_args)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/command_modules/role/custom.py", line 1080, in list_service_principal_credentials
return _get_service_principal_credentials(graph_client, app_object_id, cert)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/cli/command_modules/role/custom.py", line 1087, in _get_service_principal_credentials
app_creds = list(graph_client.applications.list_password_credentials(app_object_id))
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/msrest/paging.py", line 143, in __next__
self.advance_page()
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/msrest/paging.py", line 129, in advance_page
self._response = self._get_next(self.next_link)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/azure/graphrbac/operations/applications_operations.py", line 669, in internal_paging
'applicationObjectId': self._serialize.url("application_object_id", application_object_id, 'str'),
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/msrest/serialization.py", line 592, in url
data = self.validate(data, name, required=True, **kwargs)
File "/usr/local/Cellar/azure-cli/2.0.61/libexec/lib/python3.7/site-packages/msrest/serialization.py", line 662, in validate
raise ValidationError("required", name, True)
msrest.exceptions.ValidationError: Parameter 'application_object_id' can not be None.

# Fixed code
def _get_app_object_id_from_sp_object_id(client, sp_object_id):
    sp = client.service_principals.get(sp_object_id)
    result = list(client.applications.list(filter="appId eq '{}'".format(sp.app_id)))

    if result:
        return result[0].object_id
    raise CLIError(
        "Can't find associated application id from '{}'".format(sp_object_id)
    )