I read the python code in this repo. I think currently, it directly uses the AK for http calls. However, from the offical website, if SN verification is enabled, it should calculate the SN value and append SN in the request. Therefore, in this way, it will get "APP SN 校验失败" message.
From issue12, IP white list is prefered.
But I am still confused why SN is not prefered here. I think wrap the following code can support SN verification.
import asyncio
import hashlib
import urllib
import httpx
ak = $AK
sk = $SK
host = "https://api.map.baidu.com"
async def map_weather(name: str, arguments: dict):
"""
From map.py map_weather
"""
try:
uri = "/weather_abroad/v1/"
location = arguments.get("location", "")
district_id = arguments.get("district_id", "")
params = {
"data_type": "all",
"ak": ak,
"from": "py_mcp"
}
if not location:
params["district_id"] = f"{district_id}"
else:
params["location"] = f"{location}"
paramsArr = []
for key in params:
paramsArr.append(key + "=" + params[key])
queryStr = uri + "?" + "&".join(paramsArr)
encodedStr = urllib.request.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
rawStr = encodedStr + sk
sn = hashlib.md5(urllib.parse.quote_plus(rawStr).encode("utf8")).hexdigest()
queryStr = queryStr + "&sn=" + sn
url = host + queryStr
async with httpx.AsyncClient() as client:
response = await client.get(url)
response.raise_for_status()
result = response.json()
if result.get("status") != 0:
error_msg = result.get("message", "unknown error")
raise Exception(f"API response error: {error_msg}")
return [response.text]
except httpx.HTTPError as e:
raise Exception(f"HTTP request failed: {str(e)}") from e
except KeyError as e:
raise Exception(f"Failed to parse reponse: {str(e)}") from e
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(map_weather(name="weather", arguments={"district_id": "JPN10041030001"}))
I read the python code in this repo. I think currently, it directly uses the AK for http calls. However, from the offical website, if SN verification is enabled, it should calculate the SN value and append SN in the request. Therefore, in this way, it will get "APP SN 校验失败" message.
From issue12, IP white list is prefered.
But I am still confused why SN is not prefered here. I think wrap the following code can support SN verification.