REST Authentication with Groovy

3,033 views
Skip to first unread message

Rafael Antonio Gutiérrez Turullols

unread,
Jun 5, 2015, 11:25:16 PM6/5/15
to devtarge...@googlegroups.com
Hi,

Im trying to connect to my tp server following the instructions for Basic Authentication http://dev.targetprocess.com/rest/authentication#basic and this groovy code using the http-client lib:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.*
import org.apache.http.auth.*
import org.apache.http.impl.client.*
import org.apache.http.client.params.*
import org.apache.http.auth.params.*
import groovy.xml.*

def http = new HTTPBuilder('https://tp.server.com')

http.request(Method.GET, ContentType.TEXT ) { req ->

    uri.path = '/api/v1/UserStories/'
    headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4"
    headers.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    headers.Connection = "keep-alive"
    headers.'Authorization' = "Basic ${"username:password".bytes.encodeBase64().toString()}"

    response.success = { resp, reader ->
        assert resp.statusLine.statusCode == 200
        println "Got response: ${resp.statusLine}"
        println "Content-Type: ${resp.headers.'Content-Type'}"
        println reader.text
    }

    response.'404' = {
        println 'Not found'
    }
}

Im getting an error:

Caught: groovyx.net.http.HttpResponseException: Unauthorized
groovyx.net.http.HttpResponseException: Unauthorized

Does the basic authentication works?

Cheers,

Rafael Gutierrez

Alex Fomin

unread,
Jun 6, 2015, 3:32:19 PM6/6/15
to devtarge...@googlegroups.com, abadon.g...@gmail.com
Please ensure (using Fiddler on any other debugging proxy) that you have sent the authorization header (some clients does not send it without proper handshake from server). Also, what kind of installation do you use - on-site (hosted on your servers) or on-demand (hostsed on .tpondemand.com)?

Could you plz share the full response with headers and body?

Thanks,
Alex, Targetprocess team. 
Reply all
Reply to author
Forward
0 new messages