Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get email id from facebook using Grails social plugin

I am using spring-security-oauth-facebook:0.1 grails plugin in my application.Now i want to access person's email id but i am not able each time facebook server respond 1829812989120 for socialId ,Username, profileId and there is no option of email in returning Map object.Why it is happening.

     oauth {
        debug = true
           providers {
    facebook {
        api = org.scribe.builder.api.FacebookApi
        key = 'my-app-key'
        secret = 'secret-key'
        successUri = '/oauth/facebook/success'
        failureUri = '/oauth/facebook/failure'
        callback = "${baseURL}/oauth/facebook/callback"
        scopes = "['public_profile','email','name','user']"

      } 

    def sessionKey = oauthService.findSessionKeyForAccessToken(provider)

    if (!session[sessionKey]) {
        log.warn "No OAuth token in the session for provider '${provider}'"
        throw new OAuthLoginException("Authentication error for provider '${provider}'")
    }
    // Create the relevant authentication token and attempt to log in.

    OAuthToken oAuthToken = springSecurityOAuthService.createAuthToken(provider, 
                                                                            session[sessionKey])
    println "oAuthToken.principal = "+oAuthToken.principal.toString();
    println "oAuthToken.socialId  = "+oAuthToken.socialId;
    println "oAuthToken.properties= "+oAuthToken.properties
    println "oAuthToken.properties= "+oAuthToken.name
    println "oAuthToken.properties= "+oAuthToken.toString();

1)I ckecked all println value email is not there. please help me how to get email address of logged in user.

like image 225
atul Avatar asked Dec 19 '22 08:12

atul


1 Answers

Use this code after session created

OAuthToken oAuthToken = springSecurityOAuthService.createAuthToken(provider, 
                                                                        session[sessionKey])
Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]

println "Facebook token :"+facebookAccessToken

def facebookResource = oauthService.getFacebookResource(facebookAccessToken ,    "https://graph.facebook.com/me")

def facebookResponse = JSON.parse(facebookResource?.getBody())
like image 156
Sandeep Sharma Avatar answered Feb 24 '23 14:02

Sandeep Sharma