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

4.9k Views Asked by At

I'm trying to connect to my Cognitive Services resource but I'm getting the following error:

(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.

I created the the resource with kind CognitiveServices like this:

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

Using kind CustomVision.Training didn't work too.

I have already looked at this answer but it is no the same problem. I believe I am entering the correct credentials and endpoint.

I checked both Azure Portal and customvision.ai resource, I'm using the correct URL and key, but it is not working.

enter image description here

I even tried reseting the key but also it had no effect.

enter image description here

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()

I was also able to run it using the REST API, got HTTP 200.

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

The correct credentials object is this one:

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

Documentation updated, full discussion at #10362

1
On

You can clone this Microsoft Cognitive Services sample (UWP application) and check out Computer Vision feature in the sample. You will have to setup App Settings in the app before you proceed to check.

You can follow the below steps on how to do that through azure cli commands from bash / git bash:

Create resource group

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

Generate keys and echo the keys

Please note! jq needs to be installed to execute the commands below. If you do not want to use jq then you can just execute the az group deployment command and then search in the outputs section of the json where you will find the keys.

To get the keys with the default parameters execute the following commands

# 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

If instead you want to modify the default parameters you need to get the cognitive-keys-azure-deploy.json and cognitive-keys-azure-deploy.parameters.json files locally and execute the following commands

# 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 commands is as follows:

# 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*************************************"
    }
}

Also note that you can follow similar steps to generate CV key and endpoint only and use them in your application.