由于无效的订阅密钥或错误的 API 端点(认知服务自定义视觉),访问被拒绝

Access denied due to invalid subscription key or wrong API endpoint (Cognitive Services Custom Vision)

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

(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 也没用。

我已经看过 但这不是同一个问题。我相信我正在输入正确的凭据和端点。

我检查了 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。

您可以在应用程序中克隆 this Microsoft Cognitive Services sample (UWP application) and check out Computer Vision feature in the sample. You will have to setup 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 密钥和端点并在您的应用程序中使用它们。

正确的凭据对象是这个:

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

文档已更新,完整讨论在 #10362