
azure文档智能服务(document intelligence,原名form recognizer)是一个强大的云服务,用于从文档中提取文本、键值对、表格和结构化数据。开发者通常通过其python sdk与服务进行交互,其中认证是连接到服务的第一步。
当尝试使用Azure文档智能服务的端点(endpoint)和API密钥(key)进行认证时,可能会遇到以下错误信息:
azure.core.exceptions.HttpResponseError: (AuthenticationTypeDisabled) Key based authentication is disabled for this resource. Code: AuthenticationTypeDisabled Message: Key based authentication is disabled for this resource.
此错误明确指出,当前资源已禁用基于API密钥的认证类型。这意味着即使提供的API密钥是正确的,服务也不会接受它,因为它根本没有启用这种认证机制。
以下是导致此错误的典型Python代码示例:
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
# 请替换为您的实际端点和密钥
endpoint = "https://your-resource-name.cognitiveservices.azure.com/"
key = "YOUR_API_KEY" # 此处应填入您的API密钥
def format_bounding_region(bounding_regions):
if not bounding_regions:
return "N/A"
return ", ".join("Page #{}: {}".format(region.page_number, format_polygon(region.polygon)) for region in bounding_regions)
def format_polygon(polygon):
if not polygon:
return "N/A"
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])
def analyze_general_documents():
# 示例文档URL
docUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
# 初始化文档分析客户端
document_analysis_client = DocumentAnalysisClient(endpoint=endpoint, credential=AzureKeyCredential(key))
# 开始分析文档
poller = document_analysis_client.begin_analyze_document_from_url(
"prebuilt-document", docUrl)
result = poller.result()
# ... 后续处理结果的代码 ...
print("文档分析完成。")
if __name__ == "__main__":
analyze_general_documents()AuthenticationTypeDisabled错误并非代码语法或逻辑问题。它直接来源于Azure文档智能服务资源的配置。在Azure中,资源可以配置多种认证方式,包括基于API密钥和基于Azure Active Directory (AAD) 的认证。当此错误发生时,通常意味着:
值得注意的是,代码中 DocumentAnalysisClient(endpoint=endpoint, credential=AzureKeyCredential(key)) 的写法在API密钥认证启用时是完全正确的。
解决此问题主要有以下几个方向:
如果您的权限允许,或者您可以联系到Azure管理员,请按照以下步骤检查并修改文档智能服务资源的认证设置:
重要提示: 在企业环境中,强烈建议遵循组织的安全策略。如果策略要求禁用API密钥认证,请不要随意启用。
Azure Active Directory (AAD) 认证是更安全、更灵活的认证方式,特别适用于企业级应用。当API密钥认证被禁用时,使用AAD认证是最佳实践。
要使用AAD认证,您需要安装 azure-identity 库,并使用 DefaultAzureCredential。DefaultAzureCredential 会尝试通过多种方式(如环境变量、托管标识、Azure CLI、Visual Studio Code等)获取凭据。
步骤:
安装必要的库:
pip install azure-identity
修改Python代码: 将 AzureKeyCredential 替换为 DefaultAzureCredential。
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential # 仍然可以保留,但不再用于初始化
from azure.identity import DefaultAzureCredential # 导入DefaultAzureCredential
endpoint = "https://your-resource-name.cognitiveservices.azure.com/"
# key = "YOUR_API_KEY" # 不再直接使用API密钥
def format_bounding_region(bounding_regions):
# ... (与之前相同) ...
if not bounding_regions:
return "N/A"
return ", ".join("Page #{}: {}".format(region.page_number, format_polygon(region.polygon)) for region in bounding_regions)
def format_polygon(polygon):
# ... (与之前相同) ...
if not polygon:
return "N/A"
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])
def analyze_general_documents():
docUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
# 使用DefaultAzureCredential进行认证
credential = DefaultAzureCredential()
document_analysis_client = DocumentAnalysisClient(endpoint=endpoint, credential=credential)
poller = document_analysis_client.begin_analyze_document_from_url(
"prebuilt-document", docUrl)
result = poller.result()
# ... 后续处理结果的代码 ...
print("文档分析完成。")
if __name__ == "__main__":
analyze_general_documents()配置本地环境以使用AAD认证:
如果您不确定如何配置Azure资源,或者没有权限进行更改,最直接有效的方法是联系您的Azure管理员或IT团队。他们可以帮助您:
通过理解此错误的原因并采取相应的解决策略,您可以有效地连接到Azure文档智能服务并利用其强大的功能。
以上就是Azure文档智能服务:解决“密钥认证禁用”错误及替代方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号