【问题标题】:What is the Correct Endpoint for the Form Recognizer API?表单识别器 API 的正确端点是什么?
【发布时间】:2021-07-05 19:49:30
【问题描述】:

有关使用 Azure 表单识别器的文档似乎不清楚。发送我的请求的正确 ENDPOINT 是什么?

我正在使用 python 并按照文档使用表单识别器的预建收据模型,但没有得到预期的输出。我不确定我是否使用了正确的端点。我尝试了两件事:

  1. 阅读此tutorial,表明我需要在资源的概述页面中查找<ENDPOINT>。就我而言,它是:formextractor.cognitiveservices.azure.com。所以我尝试了这个:
import http.client, urllib.request, urllib.parse, urllib.error, base64

params = urllib.parse.urlencode({
    })

# key = '...'
headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': key,
}

source = r"https://www.w3.org/WAI/WCAG20/Techniques/working-examples/PDF20/table.pdf"
body = {"url":source}
body = json.dumps(body)

try:
    conn = http.client.HTTPSConnection('formextractor.cognitiveservices.azure.com')
    conn.request("POST", "/formrecognizer/v1.0-preview/prebuilt/receipt/asyncBatchAnalyze?s" % params, f"{body}", headers)
    response = conn.getresponse()
    data = response.read()
    operationURL = "" + response.getheader("Operation-Location")
    print ("Operation-Location header: " + operationURL)
    conn.close()
except Exception as e:
    print(e)

这会返回:

[Errno 8] nodename nor servname provided, or not known
  1. 但在API Docs 中,ENDPOINT 已经固定为westeurope.api.cognitive.microsoft.com,这是我的资源所在的位置。所以我尝试了这个:
# ... same headers, body and params as before
try:
    conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
    conn.request("POST", "/formrecognizer/v1.0-preview/prebuilt/receipt/asyncBatchAnalyze?%s" % params, f"{body}", headers)
    response = conn.getresponse()
    data = response.read()
    operationURL = "" + response.getheader("Operation-Location")
    print ("Operation-Location header: " + operationURL)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

在我看到的地方输出一个 URL:

{"error":{"code":"401","message": "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."}}

我确定我使用了正确的密钥。但无论如何,两者似乎都不起作用。你可以帮帮我吗?谢谢。

【问题讨论】:

    标签: microsoft-cognitive azure-cognitive-services


    【解决方案1】:

    您提到的初始文档似乎具有误导性。你可以在 Azure 门户的资源概述中找到资源的终结点。我的样品:

    Form Recognizer API(在撰写此答案时)托管在以下 Azure 区域:

    • 美国西部 2 - westus2.api.cognitive.microsoft.com
    • 西欧 - westeurope.api.cognitive.microsoft.com

    所以在我的情况下,它是西欧,正如你提到的,它在你的资源上是相同的。一旦你得到它,你就会得到一个401 Unauthorized

    几种可能性:

    • 您在传递标头的方式上出错(键名错误,键值错误),但根据您上面的代码看起来还可以(但我不习惯 Python)
    • 您的资源与您查询的端点不在同一地区(请仔细检查)
    • 您使用的是正确的根目录,但您的调用方式有问题

    一旦您检查了您的区域/键值,您可以从查询中删除您的?%s"%params 吗? Analyze Receipt 方法在查询字符串中没有参数(给定 documentation

    【讨论】:

    • 概览页面(您发布的图片)中我的资源的端点是formextractor.cognitiveservices.azure.com。删除资源并创建一个名为 table2text 的新资源后,显示的端点为table2text.cognitiveservices.azure.com。所以是的,该教程似乎确实具有误导性。在将端点更改为westeurope.cognitiveservices.azure.com 并访问检索到的 URL 中的 OperationID 后,我解决了这个问题。尝试 GET 进入 OperationURL 本身让我遇到了 401 错误。 (此外,文档中用于 python 3.2 的 Analyze Receipt sn-p 确实显示了参数)
    猜你喜欢
    • 2019-12-12
    • 2017-12-13
    • 2020-06-10
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多