php - Get email with facebook SDK 4.0 -
this code have. i'm trying user email can't. yesterday i've getting email correctly i've deleted file mistake. i'm missing now?
in login_fb.php i'm calling email this: echo $_session['email'];
session_start(); //added in v4.0.0 require_once 'autoload.php'; use facebook\facebooksession; use facebook\facebookredirectloginhelper; use facebook\facebookrequest; use facebook\facebookresponse; use facebook\facebooksdkexception; use facebook\facebookrequestexception; use facebook\facebookauthorizationexception; use facebook\graphobject; use facebook\entities\accesstoken; use facebook\httpclients\facebookcurlhttpclient; use facebook\httpclients\facebookhttpable; //init app app id , secret facebooksession::setdefaultapplication( '','' ); //login helper redirect_uri $helper = new facebookredirectloginhelper('http://www.myurl/facebook/fbconfig.php' ); try { $session = $helper->getsessionfromredirect(); } catch( facebookrequestexception $ex ) { //when facebook returns error } catch( exception $ex ) { //when validation fails or other local issues } if (isset($session)) { $request = new facebookrequest( $session, 'get', '/me?locale=en_us&fields=name,first_name,last_name,email,gender' ); $loginurl = $helper->getloginurl( array( 'scope' => 'email' )); $response = $request->execute(); //response $graphobject = $response->getgraphobject(); $fbid = $graphobject->getproperty('id'); // facebook id $full_name = $graphobject->getproperty('name'); // facebook full name $first_name = $graphobject->getproperty('first_name'); // facebook first name $last_name = $graphobject->getproperty('last_name'); // facebook last name $email = $graphobject->getproperty('email'); // facebook email $birthday = $graphobject->getproperty('birthday'); // facebook birthday $gender = $graphobject->getproperty('gender'); // facebook gender /* ---- session variables -----*/ $_session['fbid'] = $fbid; $_session['full_name'] = $full_name; $_session['first_name'] = $first_name; $_session['last_name'] = $last_name; $_session['email'] = $email; $_session['birthday'] = $birthday; $_session['gender'] = $gender; /* ---- header location after session ----*/ header("location: ../login_fb.php"); } else { $loginurl = $helper->getloginurl(); header("location: ".$loginurl); }
you calling $helper->getloginurl
twice. first time have email
in scope, not doing function’s return value; , second time don’t ask permissions @ all.
remove first call method completely. calling more once lead problems random state
value gets stored session. , add permission scope in remaining call, before redirect url.
fyi: automatically redirecting users login dialog considered bad practice , bad ux; while o.k. testing purposes, in real, user-facing app should trigger login when user requests it.
Comments
Post a Comment