2015-04-24 37 views
1

我想在我的grails項目中使用垂直響應api。但它給下面的錯誤grails中的垂直響應api

CODE:

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) 
    ... 
+0

你真的發送'api_token',是否正確? – Opal

+0

可能是你在標頭中發送錯誤的api標誌(或X-Auth-Token)。'授權'。你有沒有嘗試過使用[Rest Client Builder Plugin](https://grails.org/plugin/rest-client-builder)。它使調用REST調用非常簡單。 –

+0

傳遞令牌是有效的,我已經測試了它使用郵遞員擴展的鉻和它的工作沒有任何問題 –

回答

0

使用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()