Announcement Announcement Module
No announcement yet.
help with linkedin oauth Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with linkedin oauth

    Hello everyone,
    I am new to grails as well as this forum. If i have posted in the wrong spot, please excuse me. I am working on creating a grails project that works with oauth plugin (i am using SpringSource as my development environment) to connect to linkedin profile. i have a controller that connects and captures the data (using REST). Everything seems to work fine. the controller uses this:

       def response = oauthService.accessResource(
                    apiUrl, 'linkedin', [key:session.oauthToken.key, secret:session.oauthToken.secret], 'GET')
    and returns an xml stream similar to this:
    HTML Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <id>some string</id>
    my question is this: How do i parse the response variable? for instance, what if i want to pick out the first-name and do something with it? My next question would be: How do i deal with repeating data, but I will see how this one goes.

    Thank you

  • #2
    I would use XmlSlurper. For example, assuming that 'response' is an input stream:

    def data = new XmlSlurper().parse(response)
    def firstName = data.'first-name'.text()
    See the Groovy documentation for more info on XmlSlurper. And one quick note: controllers have a property called 'response' injected into them (it's the HttpServletResponse) so I recommend using a different local variable name.


    • #3

      thank you for your response. I've been looking at using XmlSlurper but i'm getting errors when i try to parse the data. if your provided example:

      def data = new XmlSlurper().parse(response)
      def firstName = data.'first-name'.text()
      print (firstName)
      'first-name' and .text are both underlined, assuming because they are not recognized. i also get the following error:

      Stacktrace follows: C:\Users\Delerium\Documents\workspace-sts-2.6.1.RELEASE\dct\<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      (The filename, directory name, or volume label syntax is incorrect)
      at Method)
      at<init>(FileInputStream.jav a:120)
      at<init>(FileInputStream.jav a:79)
      at anager.setupCurrentEntity( 3)
      at Detector.determineDocVersion(XMLVersionDetector.ja va:186)
      at nfiguration.parse(
      at nfiguration.parse(
      at er.parse(
      at tSAXParser.parse(
      at mpl$JAXPSAXParser.parse(
      at com.loudmountain.dct.profileController.processData (profileController.groovy:46)
      at com.loudmountain.dct.profileController.this$2$proc essData(profileController.groovy)
      at com.loudmountain.dct.profileController$_closure1.d oCall(profileController.groovy:25)
      at com.loudmountain.dct.profileController$_closure1.d oCall(profileController.groovy)

      what am i doing wrong? thank you for your help


      • #4
        It is to be expected that 'first-name' and .text() are underlined, that's not a problem.

        The exception probably stems from 'response' not being of the type I think it is. Is it a string rather than an input stream?


        • #5

          i apologize for any confusion. Yeah, it must be a string. the controller I'm using was part of a tutorial that didn't really explain what was going on. It's something that is returned from linkedin (object? string?). bellow is the controller that gets the xml.
          class profileController {
              def apiUrl = "" +
              "id," +
          	"first-name," +
          	"last-name" +
              def oauthService    
              def index = {
                  if (session.oauthToken == null) {
                  if (params?.apiUrl) apiUrl = params.apiUrl        
                  def linkedinResponse = oauthService.accessResource(
                          apiUrl, 'linkedin', [key:session.oauthToken.key, secret:session.oauthToken.secret], 'GET')	
          		processData(linkedinResponse) //this is where i send the item to my code to be processed.
                  render(view: 'index', model: [profileXML: linkedinResponse, apiUrl: apiUrl])
              def change = {
                  if (params?.apiUrl) {
                      println("Setting api url to " + params.apiUrl)
          	    apiUrl = params.apiUrl

          i think it may be due to first-name having a hyphen. when i use three double quotes around first-name, i dont get an error. below is my new code:
          XmlParser parser = new XmlParser()
          def myPerson = parser.parseText (linkedinResponse)  //changed response to linkedinResponse
          myPerson."""first-name""".each { 
          	print myPerson."""first-name""".value
          this does not produce an error but only prints out []
          Last edited by jasonwucinski; Jun 3rd, 2011, 01:18 PM.


          • #6
            Triple quotes do nothing different to single quotes in this case - except generate more noise. They should only be used for multi-line strings.

            This code works for me in Groovy console with the XML document that you included in your first post:

            def xml = new XmlSlurper().parseText(testXml)
            for (name in xml.'first-name') {
                println name.text()
            This will print "MyFirstName". Of course, using a loop only makes sense if there are more than one 'first-name' elements in the source XML document. With just a single person, you're better off with:

            def xml = new XmlSlurper().parseText(testXml)
            println xml.'first-name'.text()
            Also remember to always use the text() method for both elements and attributes to get the content of each as a string.

            Hope that helps.


            • #7

              that did it. thanks!