为了使用 API,您首先需要进行身份验证。为了进行身份验证,您需要检索授权端点。
要检索授权端点,请发出以下curl请求(我在本示例中使用关键Web服务,但您可以替换https://api.run.pivotal.io https://api.run.pivotal.io与您正在使用的 Cloud Foundry API 端点。
curl -H 'content-type: application/x-www-form-urlencoded;charset=utf-8' \
-H 'accept: application/json;charset=utf-8' \
https://api.run.pivotal.io/v2/info
你会得到类似这样的结果:
{
"name": "vcap",
"build": "2222",
"support": "http://support.cloudfoundry.com",
"version": 2,
"description": "Cloud Foundry sponsored by Pivotal",
"authorization_endpoint": "https://login.run.pivotal.io",
"token_endpoint": "https://uaa.run.pivotal.io",
"min_cli_version": null,
"min_recommended_cli_version": null,
"api_version": "2.36.0",
"app_ssh_endpoint": "ssh.run.pivotal.io:2222",
"app_ssh_host_key_fingerprint": "e7:13:4e:32:ee:39:62:df:54:41:d7:f7:8b:b2:a7:6b",
"logging_endpoint": "wss://loggregator.run.pivotal.io:443",
"doppler_logging_endpoint": "wss://doppler.run.pivotal.io:443"
}
抓住授权端点值,在本例中为:
https://login.run.pivotal.io https://login.run.pivotal.io
您现在需要获取身份验证令牌。发出以下curl命令替换[我的用户名] and [我的密码] and [我的授权端点]与你的价值观。请注意,您应该对您的密码进行 url 编码。
curl -H 'content-type: application/x-www-form-urlencoded;charset=utf-8' \
-H 'accept: application/json;charset=utf-8' \
-H 'authorization: Basic Y2Y6' \
-d "username=[my user name]&password=[my password]&grant_type=password" \
[my authorization endpoint]/oauth/token
您将收到如下所示的响应:
{
"access_token": "very_long_token.very_long_token.very_long_token",
"token_type": "bearer",
"refresh_token": "very_long_token.very_long_token.very_long_token",
"expires_in": 599,
"scope": "cloud_controller.read password.write cloud_controller.write openid",
"jti": "shorter_value"
}
您对access_token值感兴趣(access_token、refresh_token和jti已从本示例中的实际值更改)
现在我们终于可以使用 api 来获取有关我们应用程序的信息了。您可以使用上面提供的链接,但要使用该 api 端点,您需要应用程序的 guid。相反,我建议使用列出所有应用程序端点并在其上使用查询过滤器来获取您的应用程序信息。这是curl命令(替换[我的授权令牌]使用上一步中的身份验证令牌,替换[我的 API 端点]使用您用于 Cloud Foundry 的 api 端点,替换[我的应用程序名称]与您的应用程序的名称:
curl -H "authorization: bearer [my authorization token]" \
[my api endpoint]/v2/apps?q=name:[my app name] -X GET
您将收到一条如下所示的消息:
{
"total_results": 1,
"total_pages": 1,
"prev_url": null,
"next_url": null,
"resources": [
{
"metadata": {
"guid": "blah-blah",
"url": "/v2/apps/blah-blah",
"created_at": "time_stamp",
"updated_at": null
},
"entity": {
"name": "my-app",
"production": false,
"space_guid": "blah-blah",
"stack_guid": "blah-blah",
"buildpack": null,
"detected_buildpack": null,
"environment_json": {
},
"memory": 1024,
"instances": 3,
"disk_quota": 1024,
"state": "STOPPED",
"version": "blah-blah",
"command": null,
"console": false,
"debug": null,
"staging_task_id": null,
"package_state": "STAGED",
"health_check_type": "port",
"health_check_timeout": null,
"staging_failed_reason": null,
"staging_failed_description": null,
"diego": false,
"docker_image": null,
"package_updated_at": "time stamp",
"detected_start_command": "",
"enable_ssh": true,
"docker_credentials_json": {
"redacted_message": "[PRIVATE DATA HIDDEN]"
},
"space_url": "/v2/spaces/blah-blah",
"stack_url": "/v2/stacks/blah-blah",
"events_url": "/v2/apps/blah-blah/events",
"service_bindings_url": "/v2/apps/blah-blah/service_bindings",
"routes_url": "/v2/apps/blah-blah/routes"
}
}
]
}
您可以从消息中获取实例。如果您想在原始链接中使用 api,您可以获取metadata.guid 以在该调用中使用。
希望有帮助!