【问题标题】:Access denied due to invalid subscription key or wrong API endpoint (Cognitive Services Custom Vision)由于订阅密钥无效或 API 端点错误而拒绝访问(认知服务自定义视觉)
【发布时间】:2020-11-20 16:05:49
【问题描述】:

我正在尝试连接到我的认知服务资源,但出现以下错误:

(node:3246) UnhandledPromiseRejectionWarning: Error: Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource.

我用CognitiveServices 像这样创建了资源:

az cognitiveservices account create -n <name> -g <group> --kind CognitiveServices --sku S0 -l eastus --yes

使用 kind CustomVision.Training 也不起作用。

我已经看过this answer,但不是同样的问题。我相信我输入了正确的凭据和端点。

我检查了 Azure 门户和 customvision.ai 资源,我使用了正确的 URL 和密钥,但它不起作用。

我什至尝试重置密钥,但它没有效果。

import { TrainingAPIClient } from "@azure/cognitiveservices-customvision-training";
const { CognitiveServicesCredentials } = require("@azure/ms-rest-azure-js");

const cognitiveServiceCredentials = new CognitiveServicesCredentials("<MY_API_KEY>");
const client = new TrainingAPIClient(cognitiveServiceCredentials, "https://eastus.api.cognitive.microsoft.com");
const projects = client.getProjects()

我还能够使用 REST API 运行它,获得了 HTTP 200。

【问题讨论】:

  • 你应该解释你是如何尝试连接的,因为看起来你有一个有效的密钥,这可能是你正在使用它的方式

标签: microsoft-cognitive azure-cognitive-services azure-sdk microsoft-custom-vision azure-sdk-js


【解决方案1】:

您可以克隆this Microsoft 认知服务示例(UWP 应用程序)并查看示例中的计算机视觉功能。在继续检查之前,您必须在应用程序中设置App Settings

您可以按照以下步骤通过 bash / git bash 中的 azure cli 命令执行此操作:

创建资源组

# Create resource group, replace resouce group name and location of resource group as required
az group create -n kiosk-cog-service-keys -l westus

生成密钥并回显密钥

请注意!需要安装jq 才能执行以下命令。如果您不想使用 jq,则只需执行 az group deployment 命令,然后在 json 的输出部分中搜索您将找到密钥的位置。

要使用默认参数获取密钥,请执行以下命令

# The command below creates the cognitive service keys required by the KIOSK app, and then prints the keys
echo $(az group deployment create -n cog-keys-deploy -g kiosk-cog-service-keys --template-uri https://raw.githubusercontent.com/Microsoft/Cognitive-Samples-IntelligentKiosk/master/Kiosk/cognitive-keys-azure-deploy.json) | jq '.properties.outputs'

# If you dont have jq installed you can execute the command, and manually search for the outputs section
# az group deployment create -n cog-keys-deploy -g kiosk-cog-service-keys --template-uri https://raw.githubusercontent.com/Microsoft/Cognitive-Samples-IntelligentKiosk/master/Kiosk/cognitive-keys-azure-deploy.json

如果您想修改默认参数,您需要在本地获取cognitive-keys-azure-deploy.json 和cognitive-keys-azure-deploy.parameters.json 文件并执行以下命令命令

# Change working directory to Kiosk
cd Kiosk
  
# The command below creates the cognitive service keys required by the KIOSK app, and then prints the keys. You can modifiy the tiers associated with the generated keys by modifying the parameter values
echo $(az group deployment create -n cog-keys-deploy -g kiosk-cog-service-keys --template-file cognitive-keys-azure-deploy.json --parameters @cognitive-keys-azure-deploy.parameters.json) | jq '.properties.outputs'

# If you dont have jq installed you can execute the command, and manually search for the outputs section
# az group deployment create -n cog-keys-deploy -g kiosk-cog-service-keys --template-file cognitive-keys-azure-deploy.json --parameters @cognitive-keys-azure-deploy.parameters.json
    

上述命令的示例输出如下:

# Sample output of above command
{
    "bingAugosuggestKey1": {
        "type": "String",
        "value": "cb4******************************"
    },
    "bingSearchKey1": {
        "type": "String",
        "value": "88*********************************"
    },
    "compVisionEndpoint": {
        "type": "String",
        "value": "https://westus.api.cognitive.microsoft.com/vision/v1.0"
    },
    "compVisionKey1": {
        "type": "String",
        "value": "fa5**************************************"
    },
    "faceEndpoint": {
        "type": "String",
        "value": "https://westus.api.cognitive.microsoft.com/face/v1.0"
    },
    "faceKey1": {
        "type": "String",
        "value": "87f7****************************************"
    },
    "textAnalyticsEndpoint": {
        "type": "String",
        "value": "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0"
    },
    "textAnalyticsKey1": {
        "type": "String",
        "value": "ba3*************************************"
    }
}

另请注意,您可以按照类似的步骤仅生成 CV 密钥和端点并在您的应用程序中使用它们。

【讨论】:

  • 感谢您的回答,我昨天在 C# SDK 上进行了测试,效果也很好。我在 GitHub 上打开了问题 #10362
【解决方案2】:

正确的凭证对象是这个:

import { ApiKeyCredentials } from "@azure/ms-rest-js";

文档更新,完整讨论#10362

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多