尝试联系 Azure 中的认知服务时出现未经授权的异常消息
Unauthorized exception message when trying to contact cognitive service in Azure
我正在尝试使用 AI 工具为 Visual Studio 制作一个简单的图像分析项目。我创建了一个 Azure 认知服务作为 ComputerVision 服务。我创建它已经超过 10 分钟了,所以 api 键应该是有效的。然后我通过右键单击它创建了一个新项目。但是当我 运行 它然后我得到这个异常:
Please input image url or locate a local image file. If input is empty, example image will be used.
No url or file specified, use the example https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg
One or more errors occurred.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at GetImageDetailsService.Program.Main(String[] args) in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\Program.cs:line 27
Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.ComputerVisionErrorException: Operation returned an invalid status code 'Unauthorized'
at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPI.<DescribeImageWithHttpMessagesAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPIExtensions.<DescribeImageAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Microsoft.CognitiveServices.Samples.ComputerVisionApiExtensions.<DescribeImage>d__2.MoveNext() in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\ComputerVisionApiExtensions.cs:line 34
显然我没有被授权。是什么原因造成的?
我登录到我的 Azure 订阅并且我有资金(我使用的是 F0 层)。我还检查了区域是否正确以及 api 键是否匹配。
编辑:当我在应用程序调用中将 api 键更改为不同的内容时,我仍然遇到未经授权的异常。
Edit2:添加了调用 api.
的代码
var client = new ComputerVisionApiExtensions(ComputerVisionSubscriptionKey, ComputerVisionRegion, null);
var result = client.DescribeImage(imagePath).Result;
对于计算机视觉 API 类型的 Azure 认知服务,分析图像的请求如下所示:
POST https://{location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories&language=en
Host: {location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com
Content-Type: application/json
Ocp-Apim-Subscription-Key: {subscription-key-under-the-same-region-of-your-Computer-Vision-API}
{"url":"https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg"}
Note: You must use the same region in your REST call as you used to
get your subscription keys. For example, if you got your
subscription keys from westus, replace "westcentralus" in the URL
below with "westus".
如果您的 Azure 认知服务位于东亚,当您使用订阅密钥请求 westcentralus.api.cognitive.microsoft.com
而不是 eastasia.api.cognitive.microsoft.com
时,您可能会收到 401 访问被拒绝和以下响应:
{
"statusCode": 401,
"message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription."
}
当您的应用程序 运行 缩小此问题时,您可以使用 fiddler 来捕获网络跟踪。此外,您可以通过 Azure 门户遵循认知服务 "RESOURCE MANAGEMENT > Quick start" 下的详细教程。
我得到了同样的东西,并且认为 submit an issue 是值得的。我收到了修复的回复,对其进行了测试,一切正常!
要解决此问题,只需将 this.AzureRegion = GetRegion(region);
放入 ComputerVisionApiExtensions.cs
文件的构造函数中即可。
我正在尝试使用 AI 工具为 Visual Studio 制作一个简单的图像分析项目。我创建了一个 Azure 认知服务作为 ComputerVision 服务。我创建它已经超过 10 分钟了,所以 api 键应该是有效的。然后我通过右键单击它创建了一个新项目。但是当我 运行 它然后我得到这个异常:
Please input image url or locate a local image file. If input is empty, example image will be used.
No url or file specified, use the example https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg
One or more errors occurred.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at GetImageDetailsService.Program.Main(String[] args) in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\Program.cs:line 27
Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.ComputerVisionErrorException: Operation returned an invalid status code 'Unauthorized'
at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPI.<DescribeImageWithHttpMessagesAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPIExtensions.<DescribeImageAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Microsoft.CognitiveServices.Samples.ComputerVisionApiExtensions.<DescribeImage>d__2.MoveNext() in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\ComputerVisionApiExtensions.cs:line 34
显然我没有被授权。是什么原因造成的? 我登录到我的 Azure 订阅并且我有资金(我使用的是 F0 层)。我还检查了区域是否正确以及 api 键是否匹配。
编辑:当我在应用程序调用中将 api 键更改为不同的内容时,我仍然遇到未经授权的异常。
Edit2:添加了调用 api.
的代码var client = new ComputerVisionApiExtensions(ComputerVisionSubscriptionKey, ComputerVisionRegion, null);
var result = client.DescribeImage(imagePath).Result;
对于计算机视觉 API 类型的 Azure 认知服务,分析图像的请求如下所示:
POST https://{location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories&language=en
Host: {location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com
Content-Type: application/json
Ocp-Apim-Subscription-Key: {subscription-key-under-the-same-region-of-your-Computer-Vision-API}
{"url":"https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg"}
Note: You must use the same region in your REST call as you used to get your subscription keys. For example, if you got your subscription keys from westus, replace "westcentralus" in the URL below with "westus".
如果您的 Azure 认知服务位于东亚,当您使用订阅密钥请求 westcentralus.api.cognitive.microsoft.com
而不是 eastasia.api.cognitive.microsoft.com
时,您可能会收到 401 访问被拒绝和以下响应:
{
"statusCode": 401,
"message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription."
}
当您的应用程序 运行 缩小此问题时,您可以使用 fiddler 来捕获网络跟踪。此外,您可以通过 Azure 门户遵循认知服务 "RESOURCE MANAGEMENT > Quick start" 下的详细教程。
我得到了同样的东西,并且认为 submit an issue 是值得的。我收到了修复的回复,对其进行了测试,一切正常!
要解决此问题,只需将 this.AzureRegion = GetRegion(region);
放入 ComputerVisionApiExtensions.cs
文件的构造函数中即可。