joshbalfour / node-cognitive-services

Microsoft® Cognitive Services SDK for Node.JS
https://www.microsoft.com/cognitive-services
MIT License
105 stars 45 forks source link

Error 401 #41

Closed hbthien closed 6 years ago

hbthien commented 6 years ago

Hi,

I have the same error as the question in https://stackoverflow.com/questions/44027325/microsoft-cognitive-services-example-in-javascript with the current version 2.0.

(node:25997) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): StatusCodeError: 401 - "{ \"statusCode\": 401, \"message\": \"Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription.\" }" (node:25997) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. I am sure the api key is correct and recently generated. Could you please tell me how to fix it? I tried to follow the instruction of @miparnisari but the problem seems not solved. Thanks in advance. This is my code

const cognitiveServices = require('cognitive-services');
const parameters = {
    "mode": "proof",
    "mkt": "en-us"
}

const body = "Text=Bill+Gatas"

const spellCheckClient = new cognitiveServices.bingSpellCheck({
    apiKey: "mykey",
    endpoint: "https://westcentralus.api.cognitive.microsoft.com/face/v1.0"
})

spellCheckClient.spellCheck({
    parameters,
    body
}).then(response => {
    console.log(response);
})
miparnisari commented 6 years ago

Are you sure that the key corresponds to the bing spell check API and that the region in the endpoint matches with the region for your key?

hbthien commented 6 years ago

@miparnisari : Thanks for your comment. I have made a mistake in the link of endpoint, that is used for face API. However, even I change the key (tested with 2 keys) and the corresponding endpoint for Bing spell check API as follows but the problem is still unresolved. I am sure in this time key and endpoint are correct.

const cognitiveServices = require('cognitive-services');
const parameters = {
    "mode": "proof",
    "mkt": "en-us"
}

const body = "Text=Bill+Gatas"

const spellCheckClient = new cognitiveServices.bingSpellCheck({
    apiKey: "mykey",
    endpoint: "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck"
})

spellCheckClient.spellCheck({
    parameters,
    body
}).then(response => {
    console.log(response);
})
miparnisari commented 6 years ago

Are you still getting "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription."?

If so, there isn't much help that I can give you. There's something wrong with your key. Either it's for the wrong service or it doesn't match with the region. I don't know if there could be more root causes.