grailsoauthoauth-2.0facebook-oauthscribe

oauth facebook with grails accessing token


Its a grails project,
Facebook authentication is successful via oauth,
Now when it comes back to my controller, I want to get emailID of the logged in user,
Searched a lot, but did not find proper documentation,
I am using scribe and have following code in Config.groory

import org.scribe.builder.api.FacebookApi

oauth {
providers {
    facebook {
        api = FacebookApi
        key = 'xxxx'
        secret = 'yyyy'
        callback = "http://my-domain-name-here:8080/TestOAuth2/dashBoard/facebooklogin"
        successUri = "http://my-domain-name-here:8080/TestOAuth2/dashBoard/success"
    }
}
}

Any help much appreciated.
Thanks.


Solution

  • Try this..,.

    Config:

    import org.scribe.builder.api.FacebookApi
    ...
    oauth {
      providers {
        facebook {
          api = FacebookApi
    
          key = 'XXX'
          secret = 'YYY'
    
          scope = 'email,read_stream,publish_actions,user_birthday,publish_stream'
    
          callback = "http://localhost:8080/appName/oauth/facebook/callback"   //callback to oauth controller of oauth plugin
    
          successUri = "http://localhost:8080/appName/myController/facebookSuccess"
          failureUri = "http://localhost:8080/appName/myController/facebookFailure"
        }
      }
    }
    

    MyController:

    def facebookSuccess() {
        Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]
        def facebookResource = oauthService.getFacebookResource(facebookAccessToken, "https://graph.facebook.com/me")
        def facebookResponse = JSON.parse(facebookResource?.getBody())
    
        log.info "Email = ${facebookResponse.email}"
        ...
    }
    

    You can get working example from my git repo. Grails Oauth Plugin Demo.