postman Google Routes API X-Goog-FieldMask不工作

m0rkklqb  于 2023-11-18  发布在  Postman
关注(0)|答案(1)|浏览(112)

我正在发送一个帖子请求到

https://routes.googleapis.com/directions/v2:computeRoutes?key=APIKEY

字符串
身体作为

{
    "origin": {
        "location": {
            "latLng": {
                "latitude": 37.7749,
                "longitude": -122.4194
            }
        }
    },
    "destination": {
        "location": {
            "latLng": {
                "latitude": 34.0522,
                "longitude": -118.2437
            }
        }
    },
    "travelMode": "DRIVE",
    "routingPreference": "TRAFFIC_UNAWARE",
    "polylineQuality": "HIGH_QUALITY",
    "polylineEncoding": "POLYLINE_ENCODING_UNSPECIFIED",
    // "departureTime": "2023-07-25T12:00:00",
    // "arrivalTime": "2023-07-25T18:00:00",
    "computeAlternativeRoutes": true,
    "languageCode": "en",
    "regionCode": "US",
    "units": "IMPERIAL",
    "optimizeWaypointOrder": true,
    "extraComputations": [
        "FUEL_CONSUMPTION"
    ],
    "trafficModel": "BEST_GUESS",
    "transitPreferences": {
        "allowedTravelModes": [
            "BUS",
            "SUBWAY"
        ]
    }
}


和标题作为

key: X-Goog-FieldMask
value:  routes.distanceMeters,routes.duration


参考:https://developers.google.com/maps/documentation/routes/choose_fields#specify-field
我得到回应,

{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT"
    }
}


如果我不包括我的X-Goog-FieldMask头或使其为空。
则响应为

{
    "error": {
        "code": 400,
        "message": "FieldMask is a required parameter. See https://cloud.google.com/apis/docs/system-parameters on how to provide it. As an example, you can set the header 'X-Goog-FieldMask' to value 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline' to ask for the route distance, duration, and polyline in the response. You can also set the value to '*' in manual testing to get all the available response fields. However, using the '*' wildcard is discouraged in production.'",
        "status": "INVALID_ARGUMENT"
    }
}


即使在文档中它说x1c 0d1x https://cloud.google.com/apis/docs/system-parameters
如果我把X-Goog-FieldMask设置为绝对错误的东西,比如

key: X-Goog-FieldMask
value: routes.something


然后我得到预期的响应

{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.BadRequest",
                "fieldViolations": [
                    {
                        "field": "routes.something",
                        "description": "Error expanding 'fields' parameter. Cannot find matching fields for path 'routes.something'."
                    }
                ]
            }
        ]
    }
}


我正在使用postman发送邮件请求。
我也在curl中尝试了这个,结果是一样的。

ohtdti5x

ohtdti5x1#

我不知道你是否解决了这个问题,但我发现这个问题,而试图解决一些不同的东西,我想我会回答它。
在你的例子中,你请求optimizeWaypointOrder,但是你没有提供intermediates,这会给你给予那个错误。
您还在请求中包含了trafficModel,但是您将routingPreference设置为TRAFFIC_UNAWARE,这是冲突的,也会给您给予那个错误。
因此,您需要:

  • 使用routingPreference=TRAFFIC_AWARE_OPTIMAL以使用trafficModel
  • 删除optimizeWaypointOrder

通过进行这些更改,当我测试时,请求是成功的。

相关问题