Search code examples
ibm-cloudcloud-foundrymessage-hub

Bluemix API to retrieve the service credentials


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?


Solution

  • 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()))