Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.07k stars 1.19k forks source link

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

Closed epomatti closed 4 years ago

epomatti commented 4 years ago

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.

image

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

image

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 and also the C# SDK and it is working.

image

image

sarangan12 commented 4 years ago

@ramya-rao-a I have checked the provided steps and also my previous projects and created new projects and tested it. The service is returning the same error as "Access denied due to invalid subscription key or wrong API endpoint".

I have validated the values with the portal. The URL/Endpoint, headers are all the same. When I try using the portal, it is suceeding. In our SDK it is failing. I have also validated with previous SDK releases. All of them report the same error.

I think the service team must take a look at this issue since I do not find any issue with our side.

ghost commented 4 years ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @areddish, @tburns10.

ramya-rao-a commented 4 years ago

@areddish, @tburns10,

The latest package version is 5.1.1, but all that included was a change in the readme file.

The previous version 5.1.0 released 24 days ago had the changes from re-generating the package for the swagger update made in https://github.com/Azure/azure-rest-api-specs/pull/9522. At this time, it was working as expected. Something must have changed in the service between then and now for this issue to occur.

Can you please investigate?

areddish commented 4 years ago

The issue is with the credentials class that is being used. CustomVision doesn't use CognitiveServicesCredentialsas that has a hard coded header value that is not compatible with our API. You are using the correct credentials class in the C# example, which is why it works. You need to use the same credentials class for your JavaScript code - ApiKeyCredentials. For example:

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

const credentials = new ApiKeyCredentials({ inHeader: { "Training-key": trainingKey } });
const client = new TrainingAPIClient(credentials, "https://eastus.api.cognitive.microsoft.com");
const projects = client.getProjects()

For a more detailed sample take a look at our Quickstart For JavaScript

epomatti commented 4 years ago

@areddish thanks, I got my sample from the official @azure/cognitiveservices-customvision-training npm package documentation, we need to fix that:

//. ..
const cognitiveServiceCredentials = new CognitiveServicesCredentials(customVisionTrainingKey);
const client = new TrainingAPIClient(cognitiveServiceCredentials, customVisionTrainingEndPoint);
// ...

I tried running your code but I'm getting an error: TypeError: ApiKeyCredentials is not a constructor

It also printed my key in the console. Is it possible to implement in a way that this can't happen?

image

areddish commented 4 years ago

@epomatti My apologies, I copied the imports from your example and didn't notice that my imports are using a different msRest package.

import { ApiKeyCredentials } from "@azure/ms-rest-js";
epomatti commented 4 years ago

@areddish I didn't see it as well :grin: It works. I forked it and sent this pull request to reflect that change in the documentation.

Anything that can be done about the key being printed in the console when an exception occurs?

ramya-rao-a commented 4 years ago

Thanks @areddish! Has this always been the case or any recent change in the service?

Thanks for the PR @epomatti!

@sarangan12, Any questions you have for @areddish?

areddish commented 4 years ago

@epomatti What exception caused the key to print into the console? The above screen shot is a code generation/transpile error that is dumping the line of code. That would be expected as it's a dev environment thing using node or tsc.

@ramya-rao-a A few changes back (few months) we aligned with the cognitive services to use the credentials classes. But it's only been ApiKeyCredentials. It looks like the npm doc example has been wrong for years (several versions).

epomatti commented 4 years ago

@areddish you're right, I hardcoded it to make it short and that obviously will be printed (*facepalm).