Google maps 谷歌地图地理定位API始终返回“接收到无效的JSON有效负载”

Google maps 谷歌地图地理定位API始终返回“接收到无效的JSON有效负载”,google-maps,geolocation,google-geolocation,Google Maps,Geolocation,Google Geolocation,我能够执行Google Geolocation API文档提供的以下cURL操作 example.json 然后我在终端运行下面的程序,得到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请求执行此操作时,我得到以

我能够执行Google Geolocation API文档提供的以下cURL操作

example.json

然后我在终端运行下面的程序,得到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'
  }
}
我试着用不同的方式重写我的选项和请求体,但没有找到解决方案。我已经看到了答案,但它并没有真正提供关于获取请求的信息。这个问题指的是手机发射塔,而我正在与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
          }
        ]
      }
这是我的取件请求

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键是有效的,不受限制,我也将其用于places API,当我在cURL op中尝试我的键时,它返回了坐标响应


甚至可以使用fetch向这个API发出请求吗?我对其他选择持开放态度,我只是不能让它成为命令行请求

您需要使用JSON.stringifybody将正文的值序列化为JSON字符串。下面是示例代码和注意事项:用自己的API密钥替换字符串您的API密钥

希望这有帮助

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)
        }
    })
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)
        }});