Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
epomatti opened this issue Jul 30, 2020 · 10 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. Cognitive - Custom Vision customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@epomatti
Copy link
Contributor

  • Package Name: @azure/cognitiveservices-customvision-training
  • Package Version: 5.1.1
  • Operating system: Ubuntu 20.04
  • nodejs
    • version: v12.18.2

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

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 30, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jul 30, 2020
@ramya-rao-a ramya-rao-a added Client This issue points to a problem in the data-plane of the library. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Jul 30, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jul 30, 2020
@sarangan12
Copy link
Member

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

@ramya-rao-a ramya-rao-a added the Service Attention Workflow: This issue is responsible by Azure service team. label Jul 31, 2020
@ghost
Copy link

ghost commented Jul 31, 2020

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

@ghost ghost added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Jul 31, 2020
@ramya-rao-a
Copy link
Contributor

@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 Azure/azure-rest-api-specs#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
Copy link
Contributor

The issue is with the credentials class that is being used. CustomVision doesn't use CognitiveServicesCredentials as 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
Copy link
Contributor Author

@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
Copy link
Contributor

@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
Copy link
Contributor Author

@areddish I didn't see it as well 😁 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
Copy link
Contributor

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
Copy link
Contributor

@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
Copy link
Contributor Author

epomatti commented Jul 31, 2020

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

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. Cognitive - Custom Vision customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

5 participants
@sarangan12 @epomatti @ramya-rao-a @areddish and others