In a previous question, I can get the apiKey for interacting with the MessageHub management api.
I'm not binding to this service to a Bluemix application, so I don't have access to the VCAP_SERVICES environment variable in my application.
I would like to retreive the service credentials programatically. I think this may be a generic Bluemix cf api question rather than a MessageHub question.
How can I retrieve the service credentials using an API call?
Sadly because BlueMix runs a version of Cloud Foundry that is 6 months out of date, you can't use the List Service Keys endpoint.
Your only alternative is to bind it to some app (maybe not even a real app) to extract the credentials.
There is an argument that humans needing the credentials for a service is an anti-pattern, but there are plenty of use cases where it is necessary.
The https://apidocs.cloudfoundry.org/245/service_instances/list_all_service_keys_for_the_service_instance.html API worked for me.
Using the cf-python-client library:
from cloudfoundry_client.client import CloudFoundryClient
target_endpoint = 'https://api.ng.bluemix.net'
client = CloudFoundryClient(target_endpoint, skip_verification=False)
client.init_with_user_credentials(
ibm_id,
ibm_id_password
)
mh_service_instance = client.service_instances.get_first(name='my_service')
if mh_service_instance:
mh_service_instance_id = mh_service_instance['metadata']['guid']
print(mh_service_instance_id)
print(list(mh_service_instance.service_keys()))