# Buggy code
def _credentials(self):
    cred_type = self.module.params["auth_kind"]
    if cred_type == "application":
        credentials, project_id = google.auth.default(
            scopes=self.module.params["scopes"]
        )
        return credentials
    elif cred_type == "serviceaccount" and self.module.params.get(
        "service_account_file"
    ):
        path = os.path.realpath(
            os.path.expanduser(self.module.params["service_account_file"])
        )
        return service_account.Credentials.from_service_account_file(path).with_scopes(
            self.module.params["scopes"]
        )
    elif cred_type == "serviceaccount" and self.module.params.get(
        "service_account_contents"
    ):
        cred = json.loads(self.module.params.get("service_account_contents"))
        return service_account.Credentials.from_service_account_info(cred).with_scopes(
            self.module.params["scopes"]
        )
    elif cred_type == "machineaccount":
        return google.auth.compute_engine.Credentials(
            self.module.params["service_account_email"]
        )
    else:
        self.module.fail_json(msg="Credential type '%s' not implemented" % cred_type)


# Traceback (nValueError)
fatal: [tools-gitlab-dev]: FAILED! => {
"changed": false,
"module_stderr": "Warning: Permanently added 'XX.XXX.113.232' (ECDSA) to the list of known hosts.\r\nWarning: Permanently added 'XXX' (ECDSA) to the list of known hosts.\r\nConnection to XXX closed.\r\n",
"module_stdout": "Traceback (most recent call last):\r\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1557410508.844717-145048655204206/AnsiballZ_gcp_storage_object.py\", line 114, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1557410508.844717-145048655204206/AnsiballZ_gcp_storage_object.py\", line 106, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1557410508.844717-145048655204206/AnsiballZ_gcp_storage_object.py\", line 49, in invoke_module\r\n    imp.load_module('__main__', mod, module, MOD_DESC)\r\n  File \"/tmp/ansible_gcp_storage_object_payload_n1xrc9/__main__.py\", line 284, in <module>\r\n  File \"/tmp/ansible_gcp_storage_object_payload_n1xrc9/__main__.py\", line 154, in main\r\n  File \"/tmp/ansible_gcp_storage_object_payload_n1xrc9/__main__.py\", line 224, in fetch_resource\r\n  File \"/tmp/ansible_gcp_storage_object_payload_n1xrc9/ansible_gcp_storage_object_payload.zip/ansible/module_utils/gcp_utils.py\", line 79, in get\r\n  File \"/tmp/ansible_gcp_storage_object_payload_n1xrc9/ansible_gcp_storage_object_payload.zip/ansible/module_utils/gcp_utils.py\", line 126, in session\r\n  File \"/tmp/ansible_gcp_storage_object_payload_n1xrc9/ansible_gcp_storage_object_payload.zip/ansible/module_utils/gcp_utils.py\", line 155, in _credentials\r\n  File \"/usr/lib/python2.7/json/__init__.py\", line 339, in loads\r\n    return _default_decoder.decode(s)\r\n  File \"/usr/lib/python2.7/json/decoder.py\", line 364, in decode\r\n    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\r\n  File \"/usr/lib/python2.7/json/decoder.py\", line 380, in raw_decode\r\n    obj, end = self.scan_once(s, idx)\r\nValueError: Expecting property name: line 1 column 2 (char 1)\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def _credentials(self):
    cred_type = self.module.params["auth_kind"]
    if cred_type == "application":
        credentials, project_id = google.auth.default(
            scopes=self.module.params["scopes"]
        )
        return credentials
    elif cred_type == "serviceaccount" and self.module.params.get(
        "service_account_file"
    ):
        path = os.path.realpath(
            os.path.expanduser(self.module.params["service_account_file"])
        )
        return service_account.Credentials.from_service_account_file(path).with_scopes(
            self.module.params["scopes"]
        )
    elif cred_type == "serviceaccount" and self.module.params.get(
        "service_account_contents"
    ):
        try:
            cred = json.loads(self.module.params.get("service_account_contents"))
        except json.decoder.JSONDecodeError as e:
            self.module.fail_json(
                msg="Unable to decode service_account_contents as JSON : %s"
                % to_native(e)
            )
        return service_account.Credentials.from_service_account_info(cred).with_scopes(
            self.module.params["scopes"]
        )
    elif cred_type == "machineaccount":
        return google.auth.compute_engine.Credentials(
            self.module.params["service_account_email"]
        )
    else:
        self.module.fail_json(msg="Credential type '%s' not implemented" % cred_type)