我已经开始使用 Lucidworks Fusion (2.1.2) 并且最喜欢使用Groovy进行黑客攻击。旁注:python 'requests' 无缝处理了这个,但我很固执,不想使用 python ......
Fusion 有很有前途的API,我期待在 Groovy 中使用它。
如何使用 Groovy(以 groovy 方式)最好地连接到 Fusion 中的 Fusion 认证 API?
我尝试了几种方法(最后发现了一些无效的方法)。我欢迎关于为什么基本RESTClient对我不起作用以及其他“简单”解决方案的反馈。
这是我尝试过的:
groovyx.net.http.HTTPBuilder hb = new HTTPBuilder(FUSION_API_BASE)
hb.auth.basic(user, pass)
未授权的 401 失败(因为我相信编码)。HTTPBuilder 来自 gradle:
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
我也试过:
HttpPost httpPost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "sean"));
nvps.add(new BasicNameValuePair("password", "mypass"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
并得到:
{"code":"unauthorized"}
也试过:
String path = '/api/apollo/introspect'
URL url = new URL('http', 'corp', 8764, path)
try {
def foo = url.getContent()
log.info "Foo: $foo"
} catch (IOException ioe){
log.warn "IO ERR: $ioe"
}
它抛出了(现在预期的)IOError:401。如果有人想要更多关于我的失败的信息,请告诉我,我可能会用大量的技术细节让你厌烦。
我无耻地回答我自己的问题(如下),但希望那里的一些时髦的老师能给我一点启发。
回顾一下:有没有比我在下面找到的更好/更时髦的解决方案?