为 Azure 中的 API 创建自定义订阅密钥

问题描述 投票:0回答:1

我在不同的环境(开发、测试)中有两个 APIM 实例,我已在一个环境(测试)中为 API 生成了订阅密钥(主密钥和辅助密钥)。我想在另一个环境(开发)中对相同的 API 使用相同的订阅密钥。这可能吗?

我尝试了一些技巧,通过启用管理 API 并在邮递员中设置环境变量,并期望得到结果,但我收到 407 错误。网址如下,

https://{{serviceName}}.management.azure-api.net/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.ApiManagement/service/{{serviceName}}/subscriptions /test-1?api-version=2019-01-01

我也尝试过使用一些 powershell 命令,但这不起作用。命令如下,

$apimContext = New-AzureRmApiManagementContext -ResourceGroupName "<RGName>" -ServiceName "<apimname>"
Set-AzureRmApiManagementSubscription -Context $apimContext -SubscriptionId "<azuresubscriptionid>" -PrimaryKey "<key1>" -SecondaryKey "<key2>" -State "Active" -Scope "Service"

这显示以下错误: 设置 AzApiManagementSubscription: 错误代码:未找到资源 错误消息:找不到订阅。 请求 ID:a65db2c6-a221-49c7-b021-56tegh1234ier

我可以用这两种方法解决这个问题吗?

azure powershell postman azure-api-management
1个回答
0
投票

要使用

PATCH https://{{serviceName}}.management.azure-api.net/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.ApiManagement/service/{{serviceName}}/subscriptions/test-1?api-version=2019-01-01
URL 创建自定义订阅密钥,最初您需要生成 SAS 令牌。您可以参考此MS Docs手动或以编程方式创建SAS令牌。

对于手动创建,请导航至 APIM 实例中的 Blade 下方。

enter image description here enter image description here

补丁调用请求正文-

{
   "properties": {
     "primaryKey":"MyKey1",
     "secondaryKey":"MyKey2"
   }
 }

将生成的 SAS 令牌添加到标头中,如下所示。成功完成后,您应该会看到 204 No content

enter image description here

我可以看到 APIM 中的值已成功更新。

enter image description here

参考文献-

在 APIm 中分配自定义 API 密钥 – 尝试与众不同 (mikaelsand.se).

© www.soinside.com 2019 - 2024. All rights reserved.