0

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.

2
  • Are you using grails oauth plugin? What is your request url? Whar are you getting from facebook?
    – MKB
    Nov 8, 2013 at 18:39
  • I have edited the question and added some more info, I am not sure what is being received from facebook, but I am already logged in from browser so I assume authentication is successful.
    – nIx..
    Nov 8, 2013 at 18:56

2 Answers 2

3

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.

6
  • Thanks a lot for sharing. tried this. but getting NPE at.<br/> def facebookResource = oauthService.getFacebookResource(facebookAccessToken, "graph.facebook.com/me") <br/> and i checked facebookAccessToken is null.
    – nIx..
    Nov 8, 2013 at 19:26
  • sure. its <oauth:connect provider="facebook" id="facebook-connect-link">Login using Facebook</oauth:connect>
    – nIx..
    Nov 8, 2013 at 19:36
  • In your stackoverflow.com/questions/19852258/… question you have mentioned you are using grails 2.3.0. Right.
    – MKB
    Nov 8, 2013 at 19:39
  • 1
    Let me test it on grails 2.3.0. I am currently using grials 2.2.0.
    – MKB
    Nov 8, 2013 at 19:44
  • 1
    I am able to get the email. I follow same steps as described. Have a look to this repo github.com/manishkbharti/grailsOauthPluginDemo
    – MKB
    Nov 8, 2013 at 20:27
0

Email is not part of a Facebook public_profile. The only way to get the users e-mail address is to request extended permissions on the email field. You can do this by adding a scope to the oauth provider.

config.groovy

oauth {
   providers {
      facebook { 
          api = org.scribe.builder.api.FacebookApi
          scope = 'email'
          ...
          ...
      } 
   }
}

As an example of how to return email and various public_profile fields please see below. Take Note of: getFacebookResource params e.g. https://graph.facebook.com/me?fields=id,name,verified,age_range,email"

import grails.converters.JSON
import org.scribe.model.Token
import grails.plugin.springsecurity.oauth.OAuthToken

class SpringSecurityOAuthController {

   def oauthService

   def onSuccess = {
      // Validate the 'provider' URL. Any errors here are either misconfiguration
      // or web crawlers (or malicious users).
      if (!params.provider) {
        renderError 400, "The Spring Security OAuth callback URL must include the 'provider' URL parameter."
        return
      }

      def sessionKey = oauthService.findSessionKeyForAccessToken(params.provider)
      if (!session[sessionKey]) {
          renderError 500, "No OAuth token in the session for provider '${params.provider}'!"
          return
      }

      // Create the relevant authentication token and attempt to log in.
      OAuthToken oAuthToken = createAuthToken(params.provider, session[sessionKey])

      Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]

      def facebookResource = oauthService.getFacebookResource(facebookAccessToken ,    "https://graph.facebook.com/me?fields=id,name,verified,age_range,email")

      def facebookResponse = JSON.parse(facebookResource?.getBody())

      println facebookResponse

      ...
      ...
    }
}

public_profile (Default)

A person's public profile refers to the following properties on the user object by default:

  • id cover
  • name
  • first_name
  • last_name
  • age_range
  • link
  • gender
  • locale
  • picture
  • timezone
  • updated_time
  • verified

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.