Is it possible to add a Portal content item from a secured service?

I've successfully used the Python API to add a non-secure map service from a non-federated ArcGIS Server site (10.2.2) to a Portal (10.5.1). However if I turn security on for the map service then there doesn't seem to be an option in the API reference that allows me to specify credentials. It will still go ahead and add the service as a Portal content item but then when you try to load the layer into the Portal map viewer it prompts for the credentials (strangely the layers from the service are already loaded and drawn in the background before anything is entered in the credentials box).

Is there a way via the API to save the security credentials for the service within the content item in the same way that you can if you add the same service in manually?