0

我正在尝试遵循此文档以获取表单识别器 API,特别是识别收据的示例:

https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/client-library?pivots=programming-language-python&tabs=windows

我正在尝试以下代码:

import sys
import logging
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
import os
import azure.ai.formrecognizer

endpoint = r"https://form-recognizer-XXXXX-test.cognitiveservices.azure.com/"
form_recognizer_client = FormRecognizerClient(endpoint=endpoint, credential="XXXXXXXXX")
receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"

poller = form_recognizer_client.begin_recognize_receipts_from_url(receiptUrl)
receipts = poller.result()

并得到这个错误:

    request.http_request.headers[self._name] = self._credential.key

AttributeError: 'str' object has no attribute 'key'

我看到的不同之处在于,在示例中,端点和键被称为类的属性:

form_recognizer_client = FormRecognizerClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key))

但我看不到“自我”在哪里。来自以及该值如何不是字符串。

4

1 回答 1

1

我同意在快速入门中该密钥的来源有点不清楚。在示例中,API 密钥被设置为一个类变量(self 的来源),但您不需要这样做以使您的代码正常工作。

要成功进行身份验证,字符串 API 密钥“XXXXXXXXX”必须包装在凭据类 AzureKeyCredential 中。我已在下面更新了您的代码以执行此操作,请告诉我它是否适合您:

import sys
import logging
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
import os
import azure.ai.formrecognizer

endpoint = r"https://form-recognizer-XXXXX-test.cognitiveservices.azure.com/"
form_recognizer_client = FormRecognizerClient(endpoint=endpoint, 
    credential=AzureKeyCredential("XXXXXXXXX"))
receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for- 
    python/master/sdk/formrecognizer/azure-ai- 
    formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"

poller = form_recognizer_client.begin_recognize_receipts_from_url(receiptUrl)
receipts = poller.result()
于 2020-06-01T16:19:18.787 回答