Question

I'm laying the groundwork for a very basic Grails app that integrates with Last.fm. I'm stuck on the user authentication where I get a session key. From the documentation, it sounds like a very simple HTTP POST in the format I have below in code. I've tried every variation of the HTTPBuilder's post and request(POST) I've found but all error out with something like this:

| Server running. Browse to http://localhost:8080/GroovyLastFM
| Error 2013-05-14 19:57:10,042 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver  - MissingPropertyException occurred when processing request: [GET] /GroovyLastFM/RecentSongs/tokenChecker - parameters:
token: 452b5619f98e3b66cec11b61940af500
No such property: Method for class: GroovyLastFM.User. Stacktrace follows:
Message: No such property: Method for class: GroovyLastFM.User
Line | Method
->>   28 | getSession in GroovyLastFM.User

I don't know what else I could need to import, but obviously something is missing. Is this where the grails plugins come in? If so, what do I need to include at the app level to make HTTPBuilder work? I'm very new to grails and am not sure what merits an addition to the dependencies, or how to do it. Also, I'm on Grails 2.1.1 and am not using an IDE. Thanks!

package GroovyLastFM

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import java.security.MessageDigest
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

class User {
String token
String api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
String secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

User (String token) {
    this.token = token
    getSession()
}

def getSession() {
    String signature = md5("api_key" + api_key + "methodauth.getSessiontoken" + token + secret)
    def postbody = [token:token, api_key:api_key, method:'auth.getSession', api_sig:signature]
    def http = new HTTPBuilder("http://wx.audioscrobbler.com/2.0/")
    http.request(Method.POST) {req->
        headers.accept = "application/xml"
        requestContentType = ContentType.URLENC
        body = postbody
        response.success { resp,xml->
            // read xml response
        }
    }   
}

I did also try a basic curl post to make sure my parameters are correct, and it did return the session key as I expected:

curl -X POST -d "token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&method=auth.getSession&api_sig=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" http://ws.audioscrobbler.com/2.0/

Links:

Was it helpful?

Solution

You are importing groovyx.net.http.Method.* and using Method.POST, that's why you are getting No such property: Method.

Replace it with:

http.request(POST) { req ->  ... }

... that should do.

Alternatively, you could also change the import to:

import static groovyx.net.http.Method

and continue using Method.POST.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top