0

我已经开始使用 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。如果有人想要更多关于我的失败的信息,请告诉我,我可能会用大量的技术细节让你厌烦。

我无耻地回答我自己的问题(如下),但希望那里的一些时髦的老师能给我一点启发。

回顾一下:有没有比我在下面找到的更好/更时髦的解决方案?

4

1 回答 1

0

所以我问了这个问题并发布了我找到的解决方案。希望人们会添加更好的解决方案,甚至可以解释我在最初的尝试中错过了什么。

这是我的首选解决方案(以下所有三个解决方案均来自谷歌搜索,但我丢失了链接,请随意戳我,我会挖掘它们——向原始海报致敬):

String furl = "${FUSION_API_BASE}${path}" //http://localhost:8764/api/apollo/introspect
RESTClient rc = new RESTClient(furl)
rc.headers['Authorization'] = 'Basic ' + "$user:$pass".bytes.encodeBase64()
//rc.headers['Authorization'] = 'Basic ' + "$user:$pass".getBytes('iso-8859-1').encodeBase64()
def foo = rc.get([:])
log.info "Foo: $foo"

还有另一个可行的解决方案:

RESTClient rest = new RESTClient( 'http://localhost:8764/' )
HttpClient client = rest.client
client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'sean:mypass'.bytes.encodeBase64().toString())
    }
})
def resp = rest.get( path : path)
assert resp.status == 200  // HTTP response code; 404 means not found, etc.
println resp.getData()

对于那些在家里记分的人,一个用于比较的 python 解决方案:

import requests
from requests.auth import HTTPBasicAuth
rsp = requests.get('http://corp:8764/api/apollo/introspect', auth=HTTPBasicAuth('sean', 'lucid4pass'))
print "Response ok/status code: %s/%s", rsp.ok, rsp.status_code
print "Response content: %s", rsp.content

高温下,

肖恩

于 2016-04-03T02:38:33.740 回答