Google 地图地理位置 API 总是 returns 'Invalid JSON payload received.'
Google Maps Geolocation API always returns 'Invalid JSON payload received.'
我能够执行 Google 地理定位 API 文档提供的以下 cURL
操作。
example.json
{
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
]
}
然后我 运行 在终端下面获取 GPS 坐标。
$ curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY"
但是,当我尝试使用 fetch 将此作为 POST 请求执行时,出现以下错误。
{
error: {
code: 400,
message: 'Invalid JSON payload received. Unexpected token.\n[object Object]\n ^',
errors: [ [Object] ],
status: 'INVALID_ARGUMENT'
}
}
我尝试以不同的方式重写我的选项和请求正文,但没有找到解决方案。我已经看到 this 答案,但它并没有真正提供有关获取请求的信息。这个问题是指手机信号塔,而我正在使用 wifiAccessPoints,但我认为请求的结构是相似的。下面是我的请求体,和example.json.
一样
const body = {
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
]
}
这是我的 POST 获取请求。
var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=" + apiKey
fetch(url, {method: 'POST', headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
}, body: body})
.then(res=>res.json())
.then((json,err)=>{
if (err){
console.log(err)
} else {
console.log(json)
}
})
我的 API 密钥不受限制地有效(我也将它用于 API 的位置),当我在 cURL
操作中尝试我的密钥时,它返回了坐标响应。
甚至可以使用 fetch 向 API 发出请求吗?我对其他替代方案持开放态度,我只是不能让它成为命令行请求。
您需要使用 JSON.stringify(body)
将 body 的值序列化为 JSON 字符串。这是示例代码和一个sample fiddle(注意:将字符串YOUR_API_KEY替换为您自己的API键)。
const body = {
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
]
}
fetch("https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY", {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
//make sure to serialize your JSON body
body: JSON.stringify(body)
})
.then(res=>res.json())
.then((json,err)=>{
if (err){
console.log(err)
} else {
console.log(json)
}});
希望对您有所帮助!
我能够执行 Google 地理定位 API 文档提供的以下 cURL
操作。
example.json
{
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
]
}
然后我 运行 在终端下面获取 GPS 坐标。
$ curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY"
但是,当我尝试使用 fetch 将此作为 POST 请求执行时,出现以下错误。
{
error: {
code: 400,
message: 'Invalid JSON payload received. Unexpected token.\n[object Object]\n ^',
errors: [ [Object] ],
status: 'INVALID_ARGUMENT'
}
}
我尝试以不同的方式重写我的选项和请求正文,但没有找到解决方案。我已经看到 this 答案,但它并没有真正提供有关获取请求的信息。这个问题是指手机信号塔,而我正在使用 wifiAccessPoints,但我认为请求的结构是相似的。下面是我的请求体,和example.json.
一样const body = {
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
]
}
这是我的 POST 获取请求。
var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=" + apiKey
fetch(url, {method: 'POST', headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
}, body: body})
.then(res=>res.json())
.then((json,err)=>{
if (err){
console.log(err)
} else {
console.log(json)
}
})
我的 API 密钥不受限制地有效(我也将它用于 API 的位置),当我在 cURL
操作中尝试我的密钥时,它返回了坐标响应。
甚至可以使用 fetch 向 API 发出请求吗?我对其他替代方案持开放态度,我只是不能让它成为命令行请求。
您需要使用 JSON.stringify(body)
将 body 的值序列化为 JSON 字符串。这是示例代码和一个sample fiddle(注意:将字符串YOUR_API_KEY替换为您自己的API键)。
const body = {
"considerIp": "false",
"wifiAccessPoints": [
{
"macAddress": "00:25:9c:cf:1c:ac",
"signalStrength": -43,
"signalToNoiseRatio": 0
},
{
"macAddress": "00:25:9c:cf:1c:ad",
"signalStrength": -55,
"signalToNoiseRatio": 0
}
]
}
fetch("https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY", {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
//make sure to serialize your JSON body
body: JSON.stringify(body)
})
.then(res=>res.json())
.then((json,err)=>{
if (err){
console.log(err)
} else {
console.log(json)
}});
希望对您有所帮助!