How to get facebook email id using grails graphic plugin

I am using spring -security-oauth-facebook: 0.1 grails plugin in my application. Now I want to access the person’s email id, but I can’t answer the question about facebook server 1829812989120 for socialId, Username, profileId every time and there is no email option in the returned Map object. Why is this 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 wrote down all the println value that does not exist. please help me how to get the registered user email address.

+4
source share
2

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())
+9

public_profile. - permissions . oauth.

config.groovy

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

email various public_profile fields . . : getFacebookResource, . 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 ( )

:

  • id
  • name
  • first_name
  • last_name
  • age_range
  • picture
  • updated_time
0

Source: https://habr.com/ru/post/1569040/


All Articles