Bluemix API 检索服务凭证

Bluemix API to retrieve the service credentials

在之前的中,我可以获得api用于与MessageHub管理交互的密钥api。

我没有将此服务绑定到 Bluemix 应用程序,因此我无权访问我的应用程序中的 VCAP_SERVICES 环境变量。

我想以编程方式检索服务凭证。我认为这可能是一个通用的 Bluemix cf api 问题,而不是 MessageHub 问题。

如何使用 API 调用检索服务凭据?

很遗憾,因为 BlueMix runs a version of Cloud Foundry that is 6 months out of date, you can't use the List Service Keys 端点。

您唯一的选择是将其绑定到某个应用程序(甚至可能不是真正的应用程序)以提取凭据。

有人认为人类需要服务凭据是一种反模式,但在很多用例中都是必要的。

https://apidocs.cloudfoundry.org/245/service_instances/list_all_service_keys_for_the_service_instance.html API 对我有用。

使用 cf-python-client 库:

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