【发布时间】:2018-09-18 09:37:11
【问题描述】:
我希望 API 管理服务中的一个 API 调用 CosmosDB 中的存储过程并返回其结果。似乎没有太多关于这个主题的文档。
到目前为止我的尝试:
- 前端:带有三个参数的 GET 方法
- 入站处理:我对这个问题的代码进行了如下修改
<policies>
<inbound>
<base />
<set-variable name="Content-Type" value="application/query+json" />
<set-variable name="x-ms-documentdb-isquery" value="True" />
<set-variable name="x-ms-documentdb-query-enablecrosspartition" value="False" />
<set-variable name="x-ms-max-item-count" value="1000" />
<set-variable name="x-ms-version" value="2017-02-22" />
<set-variable name="x-ms-date" value="@( DateTime.UtcNow.ToString("R") )" />
<set-header name="Content-Type" exists-action="override">
<value>@((string)context.Variables["Content-Type"])</value>
</set-header>
<set-header name="x-ms-documentdb-isquery" exists-action="override">
<value>@((string)context.Variables["x-ms-documentdb-isquery"])</value>
</set-header>
<set-header name="x-ms-documentdb-query-enablecrosspartition" exists-action="override">
<value>@((string)context.Variables["x-ms-documentdb-query-enablecrosspartition"])</value>
</set-header>
<set-header name="x-ms-max-item-count" exists-action="override">
<value>@((string)context.Variables["x-ms-max-item-count"])</value>
</set-header>
<set-header name="x-ms-version" exists-action="override">
<value>@((string)context.Variables["x-ms-version"])</value>
</set-header>
<set-header name="x-ms-documentdb-partitionkey" exists-action="override">
<value>@("[\""+context.Subscription.Id+"\"]")</value>
</set-header>
<set-header name="x-ms-date" exists-action="override">
<value>@( (string)context.Variables["x-ms-date"] )</value>
</set-header>
<set-variable name="StringToSign" value="@(string.Format("post\nsprocs\ndbs/{myDBName}/colls/{myCollName}\n{0}\n\n", ((string)context.Variables["x-ms-date"]).ToLowerInvariant()))" />
<set-variable name="cosmosreadwritekey" value="{{MyMasterKeyInReadWriteMode}}" />
<set-variable name="SharedKey" value="@{
// https://docs.microsoft.com/en-us/rest/api/documentdb/access-control-on-documentdb-resources#constructkeytoken
System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String((string)context.Variables["cosmosreadwritekey"]));
return Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes((string)context.Variables["StringToSign"])));
}" />
<set-variable name="Authorization" value="@(string.Format("type=master&ver=1.0&sig={0}", ((string)context.Variables["SharedKey"]).Replace("&","%26").Replace("+","%2B").Replace("=","%3D")))" />
<set-header name="Authorization" exists-action="override">
<value>@((string)context.Variables["Authorization"])</value>
</set-header>
<set-backend-service base-url="https://{myCosmosName}.documents.azure.com" />
<rewrite-uri template="/dbs/{myDBName}/colls/{myCollName}/sprocs" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
- 后端:HTTPS 以以下 URL 结尾:https://{myCosmosName}.documents.azure.com/
但是,当我测试 API 时,我系统地收到以下错误消息:
输入的授权令牌无法处理请求。请检查预期的有效负载是否按照协议构建,并检查正在使用的密钥。服务器使用以下负载进行签名:'get\nsprocs\ndbs/{myDBName}/colls/{myDBName}\ntue, 18 sep 2018 09:31:58 gmt\n\n'\r\nActivityId: 64586521-cf37- 44e5-80a4-bac0a9d3b261,Microsoft.Azure.Documents.Common/2.0.0.0
我的问题:
- 如何使此调用适用于我的存储过程?
- 如何将 API 参数传递给对存储过程的调用?
【问题讨论】:
-
您应该能够在测试控制台的 Trace 选项卡中看到对 Cosmos 的实际请求。你能检查一下它看起来是否正常吗?
标签: azure azure-cosmosdb azure-api-management