【发布时间】:2014-03-11 17:46:51
【问题描述】:
我正在尝试使用 Grails RestBuilder 插件将 OAuth2 用户凭据发布到 OAuth2 服务。
如果我尝试将帖子正文指定为地图,则会收到关于 LinkedHashMap 没有消息转换器的错误。
如果我尝试将正文指定为字符串,则帖子会通过,但不会将任何变量发布到服务器操作。
这是帖子:
RestBuilder rest = new RestBuilder()
def resp = rest.post("http://${hostname}/oauth/token") {
auth(clientId, clientSecret)
accept("application/json")
contentType("application/x-www-form-urlencoded")
// This results in a message converter error because it doesn't know how
// to convert a LinkedHashmap
// ["grant_type": "password", "username": username, "password": password]
// This sends the request, but username and password are null on the host
body = ("grant_type=password&username=${username}&password=${password}" as String)
}
def json = resp.json
我也尝试在 post() 方法调用中设置 urlVariables,但用户名/密码仍然为空。
这是一个非常简单的帖子,但我似乎无法让它工作。任何建议将不胜感激。
【问题讨论】:
-
你可以尝试发送 JSON 而不是 Map 吗?
body(["grant_type": "password", "username": username, "password": password] as JSON)。理想情况下,如果内容类型是 JSON,那么您可以直接在 POST 中将正文发送为json {},而不是使用body。 -
我可能可以,但规范指定它应该是 url-formencoded:tools.ietf.org/html/rfc6749#section-4.3.2