Google QPX API post 与 JSON return "HTTP/1.1 403 Forbidden" 在 Groovy
Google QPX API post with JSON return "HTTP/1.1 403 Forbidden" on Groovy
我无法在 groovy 中消耗 Google 休息 API。
我是 groovy :S 的新手,我正在使用 HTTPBuilder 来询问服务。我的代码是:
public static void testJSONPost() {
def builder = new HTTPBuilder("https://www.googleapis.com/qpxExpress/v1/trips/search?key={$MY_WEB_KEY}")
def result = builder.request(POST, JSON) { req ->
uri.query = ["request": ["passengers": ["adultCount": 1],"slice": [["origin": "BOS","destination": "LAX","date": "2015-05-13"],["origin": "LAX","destination": "BOS","date": "2015-05-23"]]]]
response.success = {resp, json ->
println "JSON POST Success: ${resp.statusLine}"
return json.name
}
response.failure = {resp ->
println "JSON POST Failed: ${resp.statusLine}"
}
}
}
testJSONPost()
我尝试使用 curl 示例,它成功了:
curl -d @reques.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_WEB_KEY
"reques.json"的内容是:
{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2015-05-13"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2015-06-06"
}
]
}
}
我使用的密钥是网络密钥。
好吧,我会继续搜索和尝试。
在 groovy 代码中,您似乎将地图添加到 url,而不是在请求正文中发送?
要添加到 groovy 中的正文,请更改您的
uri.query = ...
行开头
body = ...
我无法在 groovy 中消耗 Google 休息 API。
我是 groovy :S 的新手,我正在使用 HTTPBuilder 来询问服务。我的代码是:
public static void testJSONPost() {
def builder = new HTTPBuilder("https://www.googleapis.com/qpxExpress/v1/trips/search?key={$MY_WEB_KEY}")
def result = builder.request(POST, JSON) { req ->
uri.query = ["request": ["passengers": ["adultCount": 1],"slice": [["origin": "BOS","destination": "LAX","date": "2015-05-13"],["origin": "LAX","destination": "BOS","date": "2015-05-23"]]]]
response.success = {resp, json ->
println "JSON POST Success: ${resp.statusLine}"
return json.name
}
response.failure = {resp ->
println "JSON POST Failed: ${resp.statusLine}"
}
}
}
testJSONPost()
我尝试使用 curl 示例,它成功了:
curl -d @reques.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_WEB_KEY
"reques.json"的内容是:
{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2015-05-13"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2015-06-06"
}
]
}
}
我使用的密钥是网络密钥。
好吧,我会继续搜索和尝试。
在 groovy 代码中,您似乎将地图添加到 url,而不是在请求正文中发送?
要添加到 groovy 中的正文,请更改您的
uri.query = ...
行开头
body = ...