Discussion:
[groovy-dev] How to authenticate when using xmlslurper
amimas
2015-02-08 21:39:23 UTC
Permalink
Hi,

I'm new to Groovy. Starting to learn Graddle and one of the task led me to
Groovy.

I need to read from an atom/rss feed. The source of the feed is on an
Intranet site, which requires authentication. It's running https protocol.
Would you be able to point me to a documentation that shows how to
authenticate or let me know how to authenticate?

I looked into Groovy's documentation as well as some online example. Maybe
I'm missing the right keyword in my search, but couldn't find the info I
need.

If you need further information, please let me know. Thank you.





--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Mario Garcia
2015-02-08 22:09:30 UTC
Permalink
Take a look at HttpBuilder
<http://groovy.codehaus.org/modules/http-builder/index.html>. There's a
specific page talking about basic authentication and still you will be
using a XmlSlurper under the hood:
A basic authentication example:
http://groovy.codehaus.org/modules/http-builder/doc/auth.html
<http://groovy.codehaus.org/modules/http-builder/doc/auth.html>
Getting responses as Xml:
http://groovy.codehaus.org/modules/http-builder/doc/xml.html

Regards
Mario
Post by amimas
Hi,
I'm new to Groovy. Starting to learn Graddle and one of the task led me to
Groovy.
I need to read from an atom/rss feed. The source of the feed is on an
Intranet site, which requires authentication. It's running https protocol.
Would you be able to point me to a documentation that shows how to
authenticate or let me know how to authenticate?
I looked into Groovy's documentation as well as some online example. Maybe
I'm missing the right keyword in my search, but couldn't find the info I
need.
If you need further information, please let me know. Thank you.
--
http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422.html
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
amimas
2015-02-08 23:00:09 UTC
Permalink
Thank you Mario for the quick reply and the reference links. I actually
looked into HttpBuilder already as well as the two links you provided. When
I try the example in the first link for basic authentication, I get the
build file 'C:\tutorials\gradle\Authentication\build.gradle': 20: unable
to re
solve class HTTPBuilder @ line 20, column 12.
def auth = new HTTPBuilder (url)

Here's what my source code looks like in my build.gradle file

dependencies { classpath
"org.codehaus.groovy.modules.http-builder:http-builder:0.5.2" }

def authSite = new HTTPBuilder('https://intranet/url/to/atom/feed/')
authSite.auth.basic 'usr', 'pwd'

What am I doing wrong? I've just started learning Gradle/Groovy and still
looking for a tutorial on https authentication, but so far those examples
didn't work in my case.





--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722424.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Pascal Schumacher
2015-02-09 17:56:56 UTC
Permalink
Hi amimas,

if you want to use dependencys like http-builder in a gradle script you
need to add the dependency to the buildscript dependencies:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath|'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'|
}
}

Take a look at: https://gradle.org/docs/current/userguide/organizing_build_logic.html "60.5. External dependencies for the build script" for details

-Pascal
Post by amimas
Thank you Mario for the quick reply and the reference links. I actually
looked into HttpBuilder already as well as the two links you provided. When
I try the example in the first link for basic authentication, I get the
build file 'C:\tutorials\gradle\Authentication\build.gradle': 20: unable
to re
def auth = new HTTPBuilder (url)
Here's what my source code looks like in my build.gradle file
dependencies { classpath
"org.codehaus.groovy.modules.http-builder:http-builder:0.5.2" }
def authSite = new HTTPBuilder('https://intranet/url/to/atom/feed/')
authSite.auth.basic 'usr', 'pwd'
What am I doing wrong? I've just started learning Gradle/Groovy and still
looking for a tutorial on https authentication, but so far those examples
didn't work in my case.
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722424.html
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
amimas
2015-02-09 21:42:56 UTC
Permalink
Hi Pascal,

Thank you for your suggestion. I've been looking into this today as well
and I was going through Gradle documentation. I also realized the same
thing you had suggested. So, here's what my gradle's build script looks
like right now.

apply plugin: 'java'
apply plugin: 'groovy'

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.codehaus.groovy.modules.http-builder', name:
'http-builder', version: '0.7.1'
}
}

// initialze a new builder and give a default URL
def http = new HTTPBuilder('http://www.google.com/search')


As you can see, I haven't really bothered writing anything else for
reading/parsing xml feed. I'm still getting the same error message on the
last line about "unable to resolve class HTTPBuilder'.


On Mon, Feb 9, 2015 at 12:57 PM, pschumacher [via Groovy] <
Post by Pascal Schumacher
Hi amimas,
if you want to use dependencys like http-builder in a gradle script you
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
}
}
Take a look at: https://gradle.org/docs/current/userguide/organizing_build_logic.html "60.5. External dependencies for the build script" for details
-Pascal
Thank you Mario for the quick reply and the reference links. I actually
looked into HttpBuilder already as well as the two links you provided. When
I try the example in the first link for basic authentication, I get the
build file 'C:\tutorials\gradle\Authentication\build.gradle': 20: unable
to re
def auth = new HTTPBuilder (url)
Here's what my source code looks like in my build.gradle file
dependencies { classpath
"org.codehaus.groovy.modules.http-builder:http-builder:0.5.2" }
def authSite = new HTTPBuilder('https://intranet/url/to/atom/feed/')
authSite.auth.basic 'usr', 'pwd'
What am I doing wrong? I've just started learning Gradle/Groovy and still
looking for a tutorial on https authentication, but so far those examples
didn't work in my case.
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722424.html
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
------------------------------
If you reply to this email, your message will be added to the discussion
http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722438.html
To unsubscribe from How to authenticate when using xmlslurper, click here
<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5722422&code=YXZlci5taW1hc0BnbWFpbC5jb218NTcyMjQyMnwtMTMwMTI3MjMwMA==>
.
NAML
<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722439.html
Sent from the groovy - dev mailing list archive at Nabble.com.
Pascal Schumacher
2015-02-09 22:42:50 UTC
Permalink
Hi Amimas,

you are just missing the import. Add:

import groovyx.net.http.HTTPBuilder

at the beginning of your script and it will work.

Here's a full working example which can be run with "gradle" or "gradle
google":

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.TEXT

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.codehaus.groovy.modules.http-builder',
name: 'http-builder', version: '0.7.1'
}
}

task(google) << {
def http = new HTTPBuilder('http://www.google.com')

http.get(path: '/search', contentType: TEXT, query: [q: 'Groovy'])
{ resp, reader ->
println "response status: ${resp.statusLine}"
println 'Headers: -----------'
resp.headers.each { h ->
println " ${h.name} : ${h.value}"
}
println 'Response data: -----'
System.out << reader
println '\n--------------------'
}
}

defaultTasks 'google'
Post by amimas
Hi Pascal,
Thank you for your suggestion. I've been looking into this today as
well and I was going through Gradle documentation. I also realized the
same thing you had suggested. So, here's what my gradle's build script
looks like right now.
apply plugin: 'java'
apply plugin: 'groovy'
buildscript {
repositories {
mavenCentral()
}
dependencies {
'http-builder', version: '0.7.1'
}
}
// initialze a new builder and give a default URL
def http = new HTTPBuilder('http://www.google.com/search')
As you can see, I haven't really bothered writing anything else for
reading/parsing xml feed. I'm still getting the same error message on
the last line about "unable to resolve class HTTPBuilder'.
On Mon, Feb 9, 2015 at 12:57 PM, pschumacher [via Groovy] <[hidden
Hi amimas,
if you want to use dependencys like http-builder in a gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath|'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'|
}
}
Take a look at:https://gradle.org/docs/current/userguide/organizing_build_logic.html "60.5. External dependencies for the build script" for details
-Pascal
Post by amimas
Thank you Mario for the quick reply and the reference links. I actually
looked into HttpBuilder already as well as the two links you provided. When
I try the example in the first link for basic authentication, I get the
build file 'C:\tutorials\gradle\Authentication\build.gradle': 20: unable
to re
def auth = new HTTPBuilder (url)
Here's what my source code looks like in my build.gradle file
dependencies { classpath
"org.codehaus.groovy.modules.http-builder:http-builder:0.5.2" }
def authSite = new HTTPBuilder('https://intranet/url/to/atom/feed/')
authSite.auth.basic 'usr', 'pwd'
What am I doing wrong? I've just started learning Gradle/Groovy and still
looking for a tutorial on https authentication, but so far those examples
didn't work in my case.
--
View this message in context:http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722424.html
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
------------------------------------------------------------------------
If you reply to this email, your message will be added to the
http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722438.html
To unsubscribe from How to authenticate when using xmlslurper,
click here.
NAML
<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
------------------------------------------------------------------------
View this message in context: Re: How to authenticate when using
xmlslurper
<http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722439.html>
Sent from the groovy - dev mailing list archive
<http://groovy.329449.n5.nabble.com/groovy-dev-f372993.html> at
Nabble.com.
amimas
2015-02-12 15:57:19 UTC
Permalink
Thank you Pascal for your reply. That's what I was missing and I was able to
run your example successfully.

Now, I can at least try to authenticate using httpbuilder. However, I'm keep
getting forbidden status. Have I made any other mistakes in the following
script?



import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.TEXT

buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath group: 'org.codehaus.groovy.modules.http-builder', name:
'http-builder', version: '0.7.1'
}
}
def http = new HTTPBuilder('https://intranet/url')

http.auth.basic 'username', 'password'
http.get(path: '/event', contentType: TEXT, query:[id:
'56c85510-c991-4ab3-9f41-86fe076bc6a1', startDate: '2015-02-05']) {
resp, reader ->
println "response status: ${resp.statusLine}"
resp.headers.each {h ->
println "${h.name}: ${h.value}"
}
}



--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722548.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Pascal Schumacher
2015-02-12 16:12:29 UTC
Permalink
Looks fine as far as I can tell.

Can you access the site without authentication? Maybe it's a HTTPS/SSL
issue? Try adding |"http.ignoreSSLIssues()|"
(http://groovy.codehaus.org/modules/http-builder/doc/ssl.html)
Post by amimas
Thank you Pascal for your reply. That's what I was missing and I was able to
run your example successfully.
Now, I can at least try to authenticate using httpbuilder. However, I'm keep
getting forbidden status. Have I made any other mistakes in the following
script?
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.TEXT
buildscript {
repositories {
mavenCentral()
}
dependencies {
'http-builder', version: '0.7.1'
}
}
def http = new HTTPBuilder('https://intranet/url')
http.auth.basic 'username', 'password'
'56c85510-c991-4ab3-9f41-86fe076bc6a1', startDate: '2015-02-05']) {
resp, reader ->
println "response status: ${resp.statusLine}"
resp.headers.each {h ->
println "${h.name}: ${h.value}"
}
}
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722548.html
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
amimas
2015-02-12 22:29:23 UTC
Permalink
Hi Pascal,

Unfortunately the site requires authentication. I tried using
http.ignoreSSLIssues() but that didn't seem to help.





--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722563.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Keith Suderman
2015-02-13 15:52:30 UTC
Permalink
If you are getting a 403 FORBIDDEN error then it means that the user does not have access to the resource. If the problem was related to the username/password you should receive a 401 UNAUTHORIZED response from the service. Is there an error message returned in the entity? Also double check with the service provider to ensure that the user credentials are correct and that the user does have access to the resource.

Keith
Post by amimas
Thank you Pascal for your reply. That's what I was missing and I was able to
run your example successfully.
Now, I can at least try to authenticate using httpbuilder. However, I'm keep
getting forbidden status. Have I made any other mistakes in the following
script?
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.TEXT
buildscript {
repositories {
mavenCentral()
}
dependencies {
'http-builder', version: '0.7.1'
}
}
def http = new HTTPBuilder('https://intranet/url')
http.auth.basic 'username', 'password'
'56c85510-c991-4ab3-9f41-86fe076bc6a1', startDate: '2015-02-05']) {
resp, reader ->
println "response status: ${resp.statusLine}"
resp.headers.each {h ->
println "${h.name}: ${h.value}"
}
}
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722548.html
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
amimas
2015-02-13 19:57:56 UTC
Permalink
Hello Keith,

Thanks for your reply. Unfortunately I'm not getting any other error
messages besides "Forbidden". And I'm sure the username and password is
correct. If I manually try to access that URL, I get prompted for
authentication and I'm able to view the Feed after authenticating manually.

I'm told that the problem is because I'm not passing any header information
and the host doesn't know if the request is coming from a browser or
something else. I tried to pass header info in http.get method, but I don't
think I'm doing it correctly. I'm getting a message saying "a problem
occurred evaluating root project HTTPBuilder.



On Fri, Feb 13, 2015 at 10:52 AM, Keith Suderman [via Groovy] <
Post by Keith Suderman
If you are getting a 403 FORBIDDEN error then it means that the user does
not have access to the resource. If the problem was related to the
username/password you should receive a 401 UNAUTHORIZED response from the
service. Is there an error message returned in the entity? Also double
check with the service provider to ensure that the user credentials are
correct and that the user does have access to the resource.
Keith
On Feb 12, 2015, at 10:57 AM, amimas <[hidden email]
Post by amimas
Thank you Pascal for your reply. That's what I was missing and I was
able to
Post by amimas
run your example successfully.
Now, I can at least try to authenticate using httpbuilder. However, I'm
keep
Post by amimas
getting forbidden status. Have I made any other mistakes in the
following
Post by amimas
script?
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.TEXT
buildscript {
repositories {
mavenCentral()
}
dependencies {
'http-builder', version: '0.7.1'
}
}
def http = new HTTPBuilder('https://intranet/url')
http.auth.basic 'username', 'password'
'56c85510-c991-4ab3-9f41-86fe076bc6a1', startDate: '2015-02-05']) {
resp, reader ->
println "response status: ${resp.statusLine}"
resp.headers.each {h ->
println "${h.name}: ${h.value}"
}
}
--
http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722548.html
Post by amimas
Sent from the groovy - dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
------------------------------
If you reply to this email, your message will be added to the discussion
http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722590.html
To unsubscribe from How to authenticate when using xmlslurper, click here
<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5722422&code=YXZlci5taW1hc0BnbWFpbC5jb218NTcyMjQyMnwtMTMwMTI3MjMwMA==>
.
NAML
<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722601.html
Sent from the groovy - dev mailing list archive at Nabble.com.
amimas
2015-02-18 16:14:28 UTC
Permalink
Thanks everyone for your help. I finally got it to work. So, it was because
of header info not passing on. The host I was trying to connect to, seems to
require this as opposed to another internet facing host that doesn't require
authentication.

After authentication, I passed this info like this:

headers.'User-Agent' = 'Mozilla/5.0'

Here's the code:

def http = new HTTPBuilder('myURL')
http.auth.basic(username, password)

http.request(GET,TEXT) { req ->
headers.'User-Agent' = 'Mozilla/5.0'

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



--
View this message in context: http://groovy.329449.n5.nabble.com/How-to-authenticate-when-using-xmlslurper-tp5722422p5722696.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...