【问题标题】:Vertical response api in grailsgrails中的垂直响应api
【发布时间】:2015-04-24 10:17:40
【问题描述】:

我正在尝试在我的 grails 项目中使用 Vertical response api。但它给出了以下错误

代码:

def http = new HTTPBuilder()

http.request('https://vrapi.verticalresponse.com/',GET, TEXT) { req ->
    uri.path =  'api/v1/contacts?type=basic'
    headers.'Authorization' = 'Bearer api_token'
    headers.'Content-Type'='application/json'


    print('====================================')
    response.success = { resp, reader ->
        assert resp.status == 200
        println "My response handler got response: ${resp.statusLine}"
        println "Response length: ${resp.headers.'Content-Length'}"
        reader // print response reader
    }

    // called only for a 404 (not found) status code:
    response.'404' = { resp ->
        println 'Not found'
    }
}

错误:

Unauthorized. Stacktrace follows:
groovyx.net.http.HttpResponseException: Unauthorized
    at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441)
    at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:417)
    ...

【问题讨论】:

  • 您发送的是api_token,对吗?
  • 可能是您在 headers.'Authorization' 中发送了错误的 api 令牌(或 X-Auth-Token)。您是否尝试过使用Rest Client Builder Plugin。它使调用 REST 调用变得非常容易。
  • 传递令牌是有效的,我已经使用 chrome 的邮递员扩展对其进行了测试,它可以正常工作
  • 移到 HttpClient 后可以正常工作,我认为问题出在 httpbuilder

标签: spring api grails groovy httpbuilder


【解决方案1】:

使用HttpClient 而不是HTTPBuilder 后,它可以正常工作。所以我认为问题出在HTTPBuilder

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet('http://vrapi.verticalresponse.com/api/v1/contacts?type=basic');

// add request header
request.addHeader("Authorization","Bearer token")

HttpResponse response = client.execute(request);

print("Response Code : "
        + response.getStatusLine().getStatusCode())

BufferedReader rd = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent()));


render rd.readLines()

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2014-09-02
    • 2014-03-05
    • 2014-07-05
    • 2017-07-24
    相关资源
    最近更新 更多