我正在尝试显示Azure API Management的REST API的订阅。根据{{3}},我应该能够在查询字符串中指定一个$filter
参数,但是我不清楚过滤器字符串应采用哪种格式,并且至少可以说得很薄。
这是我的代码:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Add("Authorization", _azureApiManagementFunctions.CreateSharedAccessToken());
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
string filter = "ownerId eq " + "/users/" + user.Id; //this is likely where the issue is
response = await client.GetAsync("/subscriptions" + "?api-version=" + apiVersion + "&$filter=" + filter);
var contents = await response.Content.ReadAsStringAsync();
var contentsJson = JsonConvert.DeserializeObject<ApimSubscription>(contents);
};
我得到一个错误:
"{\"error\":{\"code\":\"ValidationError\",\"message\":\"Invalid filter clause specified: 'ownerId eq /users/[removed]'.\",\"details\":null}}"
我还尝试将过滤器设置为"ownerId eq " + user.userId
。
所以我的问题是,以前有没有人使用过这样的filter参数,您是否对如何获得单个用户的预订有建议?
答案 0 :(得分:1)
尝试一下, 字符串过滤器=“ ownerId eq” + user.Id; 01.我可以知道,您如何获取用户ID。 02.您可以调试并发送失败的完整URL吗?
答案 1 :(得分:0)
$ filter = contains(properties / ownerId,'users / 1')
基于检查Azure Portal UI请求
在第18步中,您的代码经过了修改
如果您之前创建过服务,请跳到步骤3
然后在下一步之前转到网络点击
,否则该请求将不会重新记录
/subscriptions/xxxxxxxxxxxxxxxxxxxx/resourceGroups/stackoverflow54171031-AMS-ResourceGroup/providers/Microsoft.ApiManagement/service/stackoverflow54171031-AMS-Name/subscriptions?api-version=2018-01-01&$filter =((contains(properties / displayName,%20'jason-shave-msft')%20or%20contains(properties / stateComment,%20'jason-shave-msft')%20or%20contains(properties / userId,%20'jason-shave-msft') %20or%20包含(properties / productId,%20'jason-shave-msft')))%20和%20(properties / productId%20eq%20'starter')&$ top = 10
文本将是这样
/ subscriptions / xxxxxxxxxxxxxxxxxxxx / resourceGroups / stackoverflow54171031-AMS-ResourceGroup / 提供者/Microsoft.ApiManagement/ 服务/堆栈溢出54171031-AMS名称/ 订阅? api-version = 2018-01-01& $ filter =( (包含(properties / displayName,'jason-shave-msft')或 contains(properties / stateComment,'jason-shave-msft')或 contains(properties / userId,'jason-shave-msft')或 包含(properties / productId,'jason-shave-msft')) )和(properties / productId eq'starter')&$ top = 10
文本将是这样
$filter=
(
(
contains(properties/displayName, 'jason-shave-msft') or
contains(properties/stateComment, 'jason-shave-msft') or
contains(properties/userId, 'jason-shave-msft') or
contains(properties/productId, 'jason-shave-msft')
)
)
and
(
properties/productId eq 'starter'
)
var userid = 1;
var filter_ownerId =
$"contains(properties/ownerId, 'users/{userid}')";
var filter = $"$filter={filter_ownerId}";
Console.WriteLine(filter);
将输出
$ filter = contains(properties / ownerId,'users / 1')
var subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var resourceGroupName = "stackoverflow000000-AMS-ResourceGroup";
var serviceName = "stackoverflow000000-AMS-Name";
var baseUrl = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}"
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.Add("Authorization", _azureApiManagementFunctions.CreateSharedAccessToken());
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
var filter = $"contains(properties/ownerId, 'users/{user.Id}')";
response = await client.GetAsync($"/subscriptions?api-version={apiVersion}&$filter={filter}");
var contents = await response.Content.ReadAsStringAsync();
var contentsJson = JsonConvert.DeserializeObject<ApimSubscription>(contents);
};
答案 2 :(得分:0)
检查您使用的API版本。 https://docs.microsoft.com/en-us/rest/api/apimanagement/subscription/list#subscriptioncontract此处的文档指定了API版本2018-06-01-preview的订购合同(位于页面顶部)。实际的API规范位于以下位置:https://github.com/Azure/azure-rest-api-specs/tree/master/specification/apimanagement/resource-manager/Microsoft.ApiManagement。
最后一个稳定版本-2018-01-01-指定订阅合同具有“ userId”属性,而不具有“ ownerId”。后者已在预览版本中替换了“ userId”,并将在下一个稳定版本中保留该位置。
因此,如果您使用的版本低于2018-06-01-preview,请使用$ filter = userId eq'/ users / XXX'按用户筛选订阅。或者,在这种情况下,您也可以使用其他端点-/ users / XXX / subscriptions