Announcement Announcement Module
Collapse
No announcement yet.
help with linkedin oauth Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
       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"?>
    <person>
      <id>some string</id>
      <first-name>MyFirstName</first-name>
      <last-name>MyLastName</last-name>
    </person>
    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
    jason

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

    Code:
    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.

    Comment


    • #3
      re:

      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:

      Code:
      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:
      java.io.FileNotFoundException: C:\Users\Delerium\Documents\workspace-sts-2.6.1.RELEASE\dct\<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <person>
      <id>jIi-hRi4cI<\id>
      <first-name>Jason<\first-name>
      <last-name>Wucinski<\last-name>
      <\person>
      (The filename, directory name, or volume label syntax is incorrect)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(FileInputStream.jav a:120)
      at java.io.FileInputStream.<init>(FileInputStream.jav a:79)
      at com.sun.org.apache.xerces.internal.impl.XMLEntityM anager.setupCurrentEntity(XMLEntityManager.java:65 3)
      at com.sun.org.apache.xerces.internal.impl.XMLVersion Detector.determineDocVersion(XMLVersionDetector.ja va:186)
      at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:772)
      at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:737)
      at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(XMLParser.java:119)
      at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.parse(AbstractSAXParser.java:1205)
      at com.sun.org.apache.xerces.internal.jaxp.SAXParserI mpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
      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)
      at java.lang.Thread.run(Thread.java:662)


      what am i doing wrong? thank you for your help
      jason

      Comment


      • #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?

        Comment


        • #5
          re:

          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.
          Code:
          class profileController {
              def apiUrl = "http://api.linkedin.com/v1/people/~:(" +
              "id," +
          	"first-name," +
          	"last-name" +
          	")" 
          
          
              def oauthService    
              def index = {
                  if (session.oauthToken == null) {
                      redirect(uri:"/")
                  }
                  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
          		
                  }
                  
                  redirect(action:index,params:params)
              }

          edit:
          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:
          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, 12:18 PM.

          Comment


          • #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:

            Code:
            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:

            Code:
            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.

            Comment


            • #7
              re

              that did it. thanks!

              Comment

              Working...
              X