[英]Operation returned an invalid status code 'unauthorized' on azure cognitive
我已经从官方微软认知 github 存储库下载了这段代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Microsoft.Azure.CognitiveServices.Samples.ComputerVision.BatchReadFile
{
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
class Program
{
static void Main(string[] args)
{
// Add your Computer Vision subscription key and endpoint to your environment variables
string subscriptionKey = "my key0001"; // Environment.GetEnvironmentVariable("my key0001");
string endpoint = "https://controllo.cognitiveservices.azure.com/"; // Environment.GetEnvironmentVariable("https://controllo.cognitiveservices.azure.com/");
try
{
BatchReadFileSample.RunAsync(endpoint, subscriptionKey).Wait(5000);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("\nPress ENTER to exit.");
Console.ReadLine();
}
}
public class BatchReadFileSample
{
public static async Task RunAsync(string endpoint, string key)
{
ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{
Endpoint = endpoint
};
const int numberOfCharsInOperationId = 36;
// string localImagePath = @"Images\handwritten_text.jpg"; // See this repo's readme.md for info on how to get these images. Alternatively, you can just set the path to any appropriate image on your machine.
string localImagePath = @"C:\Users\marco.panza\OneDrive - Accenture\Desktop\Sorgenti\OCR C#\info.png";
// string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/printed_text.jpg";
Console.WriteLine("Text being batch read ...");
await BatchReadFileFromStreamAsync(computerVision, localImagePath, numberOfCharsInOperationId);
// await BatchReadFileFromUrlAsync(computerVision, remoteImageUrl, numberOfCharsInOperationId);
}
// Read text from a remote image
private static async Task BatchReadFileFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, int numberOfCharsInOperationId)
{
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
{
Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
return;
}
// Start the async process to read the text
BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync(imageUrl);
await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
}
// Recognize text from a local image
private static async Task BatchReadFileFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId)
{
if (!File.Exists(imagePath))
{
Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
return;
}
using (Stream imageStream = File.OpenRead(imagePath))
{
// Start the async process to recognize the text
BatchReadFileInStreamHeaders textHeaders = await computerVision.BatchReadFileInStreamAsync(imageStream);
await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
}
}
// Retrieve the recognized text
private static async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation, int numberOfCharsInOperationId)
{
// Retrieve the URI where the recognized text will be
// stored from the Operation-Location header
string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId);
// Wait for the operation to complete
int i = 0;
int maxRetries = 10;
while ((result.Status == TextOperationStatusCodes.Running ||
result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
{
Console.WriteLine("Server status: {0}, waiting {1} seconds...", result.Status, i);
await Task.Delay(1000);
result = await computerVision.GetReadOperationResultAsync(operationId);
}
// Display the results
Console.WriteLine();
var recResults = result.RecognitionResults;
foreach (TextRecognitionResult recResult in recResults)
{
foreach (Line line in recResult.Lines)
{
Console.WriteLine(line.Text);
}
}
Console.WriteLine();
}
}
}
但我收到此错误:
发生一个或多个错误。 (操作返回无效状态代码“未授权”)
密钥和终点是正确的(出于安全原因,我发布了一个伪密钥)。
我第一次尝试使用此代码:
string subscriptionKey = Environment.GetEnvironmentVariable("my key0001");
string endpoint = Environment.GetEnvironmentVariable("https://controllo.cognitiveservices.azure.com/");
但是这些字符串返回 null 并且在我尝试直接分配值之后:
string subscriptionKey = "my key0001");
string endpoint = "https://controllo.cognitiveservices.azure.com/);
我得到这个错误:
"One or more errors occurred. (Operation returned an invalid status code 'Unauthorized')"
有人能帮助我吗?
请确保您已经创建了当前类型的认知服务,我建议您创建All Cognitive Services
,如下所示:
您可以按照此文档创建它(多服务资源)。
我通过这项服务对我进行了一些测试,一切都按预期对我有用:
我的本地测试图像:
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.