Skip to content

Server autentification not working for "post_asset_administration_shell" and "delete_asset_administration_shell_by_id" #16

Closed
@HeikoBaum

Description

@HeikoBaum

Below you will find some code snippets where the API calls do not work, but the manual commands do.

  from aas_python_http_client import ApiClient, Configuration, \
      AssetAdministrationShellRepositoryAPIApi,
  from aas_python_http_client.util import string_to_base64url
  
  configuration = Configuration()
  configuration.host = "https://fluid40.imd.mw.tu-dresden.de:7443"
  configuration.username = "here_is_my_username"
  configuration.password = "here_is_my_password"

  file_name = f"{source_path}/{lp['aas_id_short']}.aasx"

  if use_api_calls:
      api_client = ApiClient(configuration=configuration)
      aasRepoClient = AssetAdministrationShellRepositoryAPIApi(api_client=api_client)
      aasRepoClient.post_asset_administration_shell(file_name)
  else:
      url = (f"{configuration.host}/packages?aasIds="
             f"{string_to_base64url(lp['aas_id_short'])}")

      with open(file_name, "rb") as file:
          files = {"file": file}
          headers = {
              "Content-Type": "multipart/form-data",
              "Accept": "application/json"
          }
          response = requests.post(
              url,
              files=files,
              auth=(
                  configuration.username,
                  configuration.password
              )
          )

  if use_api_calls:
        api_client = ApiClient(configuration=configuration)
        aasRepoClient = AssetAdministrationShellRepositoryAPIApi(api_client=api_client)
        aasRepoClient.delete_asset_administration_shell_by_id(
            string_to_base64url(shell_to_delete),
            async_req=True
        )
    else:
        url = (f"{configuration.host}/packages?aasId="
               f"{string_to_base64url(shell_to_delete)}&cursor=%2522%2522")
        response = requests.get(url, auth=(configuration.username, configuration.password))

        if response.status_code == 200:
            try:
                response_json = response.json()
                if "result" in response_json and len(response_json["result"]) > 0:
                    package_id = response_json["result"][0].get("packageId")
                    log.info(f"Retrieved Package ID: {package_id}")
                    if package_id:
                        delete_endpoint = (f"{configuration.host}/packages/"
                                           f"{string_to_base64url(package_id)}")
                        delete_response = requests.delete(
                            delete_endpoint, auth=(
                                configuration.username,
                                configuration.password,
                            )
                        )
                        if delete_response.status_code == 204:
                            log.info(f"Package with ID {package_id} was successfully deleted.")
                        else:
                            log.info(f"Error deleting the package {package_id}.")
                            log.info(f"Status Code: {delete_response.status_code}")
                            log.info(f"Server Response: {delete_response.text}")
                    else:
                        log.info("Error: No package ID found in response.")
                else:
                    log.info("No Packages found.")
            except json.JSONDecodeError:
                log.info("Repsonse is not a valid JSON.")
        else:
            log.info(f"Error retrieving packages. Status Code: {response.status_code}")
            log.info(f"Server Response: {response.text}")

The error message is:

aas_python_http_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Server': 'nginx', 'Date': 'Tue, 11 Feb 2025 17:46:07 GMT', 'Content-Type': 'text/html', 'Content-Length': '172', 'Connection': 'keep-alive', 'WWW-Authenticate': 'Basic realm="Authorized users only"', 'Strict-Transport-Security': 'max-age=63072000'})
HTTP response body: b'<html>\r\n<head><title>401 Authorization Required</title></head>\r\n<body>\r\n<center><h1>401 Authorization Required</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n'

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions