How to retrieve values ​​from the JSON object and authenticate it using the google-plus plugin: 0.1

advertisements

I am using google-plus:0.1 plugin in a Grails Application to enable login using Google+ ,I have successfully logged in and I am getting JSON object back.

Inside plugin there is a code where they are turning JSON string to JSON object and returning back to me. This is the code inside a plugin:-

public Person getCurrentUserProfile() {
    Person person
    try {
        URL url = new URL("https://www.googleapis.com/plus/v1/people/me?access_token=${this.accessToken}")
        def jsonString = JSON.parse(url?.text)
        println "URL : " + url.toString()
        println "JSON : " + jsonString

        person = Person.parseJSONForPerson(jsonString)
    } catch (GooglePlusException e) {
        e.printStackTrace()
    }
    return person
}

From here I am getting JSON object back Json string printed in console is:-

URL : https://www.googleapis.com/plus/v1/people/me?access_token=ya29.lgFPSagK5lQ8l19hWuxra8AxsXzZjxlbCetP-aWpUHbq2_djrdvj-3XMFMEgl9r4WfaiLvo7Q3RfJQ
JSON : [etag:"RqKWnRU4WW46-6W3rWhLR9iFZQM/eUT2aym4R39YBu_ra8ZlhztXJ6w", organizations:[[startDate:1995, title:Science, primary:false, name:St.Joseph's Convent, endDate:2008, type:school]], image:[isDefault:false, url:https://lh5.googleusercontent.com/-kuh1_bl_8is/AAAAAAAAAAI/AAAAAAAAADA/ZZfY8ob2QrQ/photo.jpg?sz=50], isPlusUser:true, emails:[[value:[email protected], type:account]], kind:plus#person, url:https://plus.google.com/102332421579091283598, id:102332421579091283598, verified:false, name:[familyName:Sahu, givenName:Kartiki], domain:crowdera.co, circledByCount:9, gender:female, displayName:Kartiki Sahu, objectType:person]

I am unable to fetch emails from the object which I am getting.

I tried fetching familyName as person.name.familyName I got it's value, but for givenName it is showing null and for emails and organisation it is showiing empty list like [].

My code :-

def code = request.getParameter("code")
    String accessToken = accessTokenService.generateAccessToken(code)
    googlePlusService.accessToken = accessToken
    def person = googlePlusService.getCurrentUserProfile()

Please help.


You can get value like this .

      String str="""{
 "kind": "plus#person",
 "etag": "\\"RqKWnRU4WW46-6W3rWhLR9iFZQM/eUT2aym4R39YBu_ra8ZlhztXJ6w\\"",
 "gender": "female",
 "emails": [
  {
   "value": "[email protected]",
   "type": "account"
  }
 ],
 "objectType": "person",
 "id": "102332421579091283598",
 "displayName": "Kartiki Sahu",
 "name": {
  "familyName": "Sahu",
  "givenName": "Kartiki"
 },
 "url": "https://plus.google.com/102332421579091283598",
 "image": {
  "url": "https://lh5.googleusercontent.com/-kuh1_bl_8is/AAAAAAAAAAI/AAAAAAAAADA/ZZfY8ob2QrQ/photo.jpg?sz=50",
  "isDefault": false
 },
 "organizations": [
  {
   "name": "St.Joseph's Convent",
   "title": "Science",
   "type": "school",
   "startDate": "1995",
   "endDate": "2008",
   "primary": false
  }
 ],
 "isPlusUser": true,
 "circledByCount": 9,
 "verified": false,
 "domain": "crowdera.co"
}
"""
        def jsonString  = new JsonSlurper().parseText(str)
        render jsonString.emails.value
    }

I am getting output as a following list .

['[email protected]']