Bluemix Cloud Foundry REST API

2019-07-29 05:22发布

问题:

Can I access Cloud Foundry REST API on Bluemix? If yes, how can I access it (cannot find any documentation)?

回答1:

You can access the Cloud Foundry REST API on Bluemix as you would normally do with CF. In addition to that, if you need it and you are already familiar with cf curl you can take a look at the bluemix curl command. For example if you want to retrieve the information for all organizations of the current account:

bluemix curl /v2/organizations

Please see the Docs for more information.



回答2:

In order to access CF API you have to get the authentication token. Then add it to each request in the headers.

oauthTokenResponse = requests.post(
  f'https://login.ng.bluemix.net/UAALoginServerWAR/oauth/token?grant_type=password&client_id=cf',
  data={'username': <your username>, 'password': <your password>, 'client_id': 'cf'},
  auth=('cf', '')
)
auth = oauthTokenResponse.json()['token_type'] + ' ' + oauthTokenResponse.json()['access_token']

appsResponse = requests.get(f'{self.api_endpoint}/v2/apps',
  headers={'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': auth}
)

apps = json.loads(appsResponse.content)