如何通过requests.get调用JSON脚本从Alloy获取表格?403错误求助
Alloy AQS请求403错误排查与解决
我尝试用Python向Alloy发送AQS查询请求获取数据,代码如下:
import json import requests aqs = {"aqs": { "type": "Join", "properties": { "attributes": [ "attributes_itemsTitle", "attributes_itemsSubtitle" ], "collectionCode": [ "Live" ], "dodiCode": "designs_nlpgPremises", "joinAttributes": [] }, "children": [ { "type": "And", "children": [ { "type": "Equals", "children": [ { "type": "Attribute", "properties": { "attributeCode": "attributes_premisesPostcode" } }, { "type": "String", "properties": { "value": [ "BS5 6AP" ] } } ] } ] } ] }} headers = { 'X-Authentication-Token' : token } urlBase = 'https://uk.alloyapp.io/api/' requestURL = urlBase + 'aqs/join?token=' + token response = requests.get(requestURL, headers = headers, data=json.dumps(aqs)) print(response.text)
运行后收到403错误,内容翻译如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <TITLE>ERROR: 请求无法被满足</TITLE> </HEAD><BODY> <H1>403 ERROR</H1> <H2>请求无法被满足。</H2> <HR noshade size="1px"> 该分发配置不允许使用当前的HTTP请求方法,仅支持可缓存请求。 目前无法连接到该应用或网站的服务器,可能是流量过大或配置错误,请稍后重试或联系应用/网站所有者。 <BR clear="all"> 如果您通过CloudFront向客户提供内容,可查看CloudFront文档排查并预防此错误。 <BR clear="all"> <HR noshade size="1px"> <PRE> 由CloudFront生成 请求ID: txrBxRox0BvgpLAFSbqvBztjhYsUmUzWdjizayD20tinETqt85fG== </PRE> <ADDRESS> </ADDRESS> </BODY></HTML>
错误原因分析
- GET请求本身是可缓存的,但你在GET请求里通过
data参数传递了请求体,这种不符合HTTP规范的请求被CloudFront拦截。 - Alloy的AQS接口设计上通常要求用POST方法传递复杂查询体,而非GET。
- 同时在URL参数和请求头里传递token,可能导致认证逻辑冲突。
修正方案
- 将请求方法改为POST,匹配AQS接口的预期调用方式。
- 使用
json参数直接传递Python字典,requests会自动完成JSON序列化并设置正确的Content-Type请求头,无需手动调用json.dumps。 - 只保留一种token传递方式(推荐用请求头,更安全规范)。
修正后的代码:
import requests aqs = {"aqs": { "type": "Join", "properties": { "attributes": [ "attributes_itemsTitle", "attributes_itemsSubtitle" ], "collectionCode": [ "Live" ], "dodiCode": "designs_nlpgPremises", "joinAttributes": [] }, "children": [ { "type": "And", "children": [ { "type": "Equals", "children": [ { "type": "Attribute", "properties": { "attributeCode": "attributes_premisesPostcode" } }, { "type": "String", "properties": { "value": [ "BS5 6AP" ] } } ] } ] } ] }} headers = { 'X-Authentication-Token' : token } urlBase = 'https://uk.alloyapp.io/api/' requestURL = urlBase + 'aqs/join' response = requests.post(requestURL, headers=headers, json=aqs) print(response.text)
内容的提问来源于stack exchange,提问作者craig3656
相关产品推荐
相关产品推荐

