从API管理服务调用CosmosDB的存储过程

时间:2018-09-18 09:37:12

标签: azure azure-cosmosdb azure-api-management

我希望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>

<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,2018年9月18日09:31:58 gmt \ n \ n'\ r \ nActivityId:64586521-cf37- 44e5-80a4-bac0a9d3b261,Microsoft.Azure.Documents.Common / 2.0.0.0

我在那里的问题:

  • 如何使此调用适用于我的存储过程?
  • 如何将API参数传递给对存储过程的调用?

1 个答案:

答案 0 :(得分:0)

要发送参数,请查看this。设置授权令牌似乎有些错误,请参阅this