Java版Azure Computer Vision快速入门示例运行报错:无法提取操作ID
问题排查方案
你可以按优先级依次排查以下问题:
1. 先确认operationLocation是否有有效值
你代码里已经打印了Operation Location:的日志,先查看该日志的输出:
- 如果输出为空,说明Read API调用未成功,优先排查以下配置问题:
- 确认
ComputerVisionClient初始化时传入的密钥、区域终结点完全和Azure门户中你创建的认知服务资源一致,不要混淆多资源的密钥,也不要写错终结点的区域后缀 - 确认你使用的本地PDF文件符合接口限制:免费层单文件大小不超过4MB,付费层不超过500MB,页数不超过2000页,且文件没有损坏
- 确认你的设备IP已被添加到认知服务资源的防火墙允许列表中,未被拦截请求
- 确认你使用的Java SDK版本和官方示例匹配,不兼容的版本会导致响应头解析失败
- 确认
2. 修复operationId提取逻辑(最高发问题)
当前新版Azure Read API返回的operationLocation会附带查询参数,格式类似:https://<你的区域>.cognitiveservices.azure.com/computervision/read/3.0/analyzeResults/xxxx-xxxx-xxxx-xxxx?api-version=3.0,你原代码直接按/分割取最后一段时,会把?api-version=xxx也带进去,不是纯UUID,导致提取失败。
直接替换extractOperationIdFromOpLocation方法为以下代码即可:
/** * Extracts the OperationId from a Operation-Location returned by the POST Read operation * @param operationLocation * @return operationId */ private static String extractOperationIdFromOpLocation(String operationLocation) { if (operationLocation != null && !operationLocation.isEmpty()) { // 先移除末尾的查询参数 String opPath = operationLocation.split("\\?")[0]; String[] splits = opPath.split("/"); if (splits != null && splits.length > 0) { return splits[splits.length - 1]; } } throw new IllegalStateException("Something went wrong: Couldn't extract the operation id from the operation location"); }
3. 其他排查点
如果以上步骤都无效,可以确认你创建的认知服务资源是否已开通OCR/Read接口的调用权限,免费层的调用配额是否已经耗尽。
内容的提问来源于stack exchange,提问作者Maik
相关产品推荐
相关产品推荐

