如何使用 REST API 访问分析数据。
准备工作
API Connect REST API 调用通过在授权标头中使用承载标记来验证。 使用工具箱凭证来请求不记名令牌。 工具包证书可在
API 管理器用户界面中找到:
- 登录到 API Manager UI。
- 从主页中,单击 下载工具箱 磁贴。
- 下载 Toolkit 凭证。
- 打开下载的
credentials.json 文件:...
"toolkit": {
"endpoint": "https://mgmt.api.example.com/api",
"client_id": "7409693f-f726-48b4-8909-7c0d26f13e81",
"client_secret": "5feeb0be-17f8-41a4-96d6-d40f33d69ef6"
},
...
记下此文件中的 toolkit.client_id, toolkit.client_secret和 toolkit.endpoint 。 您可以使用这些值来获取不记名令牌。
过程
- 创建 API 密钥。 有关如何创建 API 密钥的更多信息,请参阅管理平台 REST API 密钥。
- 使用凭证来请求不记名令牌:
curl -v -k -X POST -d '{"api_key": "<api_key>", "client_id": "<client_id>", "client_secret": "<client_secret>", "grant_type": "api_key"}' -H 'Content-Type: application/json' -H 'Accept: application/json' https://<management_server_platform_api_endpoint>/api/token
其中:
<api-key> 是您生成的 API 密钥。
<client_id> 是 credentials.json 文件 "工具包 "部分中的客户端 ID,或由 API Connect 云管理员提供。
<client_secret> 是 credentials.json 文件 "工具包 "部分中的客户机秘密,或由 API Connect 云管理员提供。
将在 access_token 属性中返回不记名令牌:
{
"access_token": "<bearer_token>",
"token_type": "Bearer",
"expires_in": 28800
}
请注意 expires_in 值,即令牌到期前的秒数。 到期后,必须请求新的不记名令牌。
- 使用返回的不记名令牌来调用分析 REST API:
# This call to /orgs/<provider_organization>/events requires a bearer token that was requested with provider org credentials:
curl -k -H 'Authorization: Bearer <bearer_token>' -X GET --url 'https://<platform api endpoint>/analytics/<analytics_service>/orgs/<provider_organization>/events'
{
"total": 300,
"search_time": 3,
"events": [...]
}
# This call to the consumer API: orgs/<consumer org>/dashboard, requires a bearer token that was requested with consumer org credentials.
curl -k -H 'X-IBM-Consumer-Context: <provider org>.<catalog>' -H 'Authorization: Bearer <bearer_token>' -X GET --url 'https://<consumer api endpoint>/consumer-analytics/orgs/<consumer org>/dashboard'
{
"search_time": 16,
"status_codes": {
"total": 300,
"data": [...]
},
"min_response_time": {
"data": ...
...
# This call to /cloud/events requires a bearer token that was requested with admin credentials:
curl -k -H 'Authorization: Bearer <bearer_token>' -X GET --url 'https://<platform api endpoint>/analytics/<analytics_service>/cloud/events'
{
"total": 45543,
"search_time": 36,
"events": [...]
其中:
<platform api endpoint> 是 toolkit.endpoint,但末尾的 /api 将替换为 /analytics 以访问分析 API。
<analytics_service> 是分析服务的名称。
- <bearer_token> 是步骤 2 中的令牌。 请勿使用引号将令牌换行。
- 使用查询参数过滤结果,只显示某些字段。
如果要搜索特定的分析事件记录,可在 API 调用中添加查询参数。 例如,只获取调用特定 API 和产品的事件数据:
/events?api_name=<api name>&product_name=<product name>
要在输出中只获取某些 API 事件记录字段,则添加
fields 查询参数。 例如,只获取 API 名称和调用时间:
/events?fields=api_name,datetime
要只请求某个时间之前或之后的事件,请使用
start 和
end 参数。 例如:
/events?start=2024-07-11T10:01:00.000Z&end=2024-07-11T10:02:00.000Z
后续操作
有关分析 REST API 的完整文档,请点击此处: 分析 REST API。