Kanishka Panamaldeniya Kanishka Panamaldeniya - 1 month ago 11
PHP Question

requesting user permissions not working facebook graph api

hi i am using facebook graph api PHP SDK . here i am trying to get the logged in user's facebook details , currently i am getting only the basic details which are not required any permissions .

Array
(
[id] => xxxxxxxxx
[name] => Kanishka Bandara Panamaldeniya
[first_name] => Kanishka
[middle_name] => Bandara
[last_name] => Panamaldeniya
[link] => http://www.facebook.com/kanishka.panamaldeniya
[username] => kanishka.panamaldeniya
[hometown] => Array
(
[id] => 108014839219027
[name] => Kandy
)

[location] => Array
(
[id] => 108602292505393
[name] => Colombo, Sri Lanka
)

[quotes] => every action has a reaction....
[work] => Array
(
[0] => Array
(
[employer] => Array
(
[id] => 234117399957729
[name] => Cyber LMJ
)

[location] => Array
(
[id] => 108602292505393
[name] => Colombo, Sri Lanka
)

[position] => Array
(
[id] => 109542932398298
[name] => Software Engineer
)

[start_date] => 2011-08
)

[1] => Array
(
[employer] => Array
(
[id] => 235852306375
[name] => eMarketingEye
)

[position] => Array
(
[id] => 109542932398298
[name] => Software Engineer
)

[start_date] => 2010-09
[end_date] => 2011-08
)

)

[sports] => Array
(
[0] => Array
(
[id] => 103992339636529
[name] => Cricket
)

)

[favorite_teams] => Array
(
[0] => Array
(
[id] => 191485640872790
[name] => Sri Lanka Cricket Team
)

)

[favorite_athletes] => Array
(
[0] => Array
(
[id] => 185101794860886
[name] => Kumara Sangakkara
)

[1] => Array
(
[id] => 119473804782315
[name] => Angelo Mathews
)

[2] => Array
(
[id] => 164574093575397
[name] => Kumar Sangakkara
)

[3] => Array
(
[id] => 169411436417102
[name] => Virat Kohli
)

[4] => Array
(
[id] => 166273026737204
[name] => Lahiru Thirimanne
)

[5] => Array
(
[id] => 101237886616765
[name] => Dinesh Chandimal
)

)

[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 34663330581
[name] => Dharmaraja College-Kandy
)

[year] => Array
(
[id] => 138383069535219
[name] => 2005
)

[type] => High School
)

[1] => Array
(
[school] => Array
(
[id] => 112872282058324
[name] => University of Colombo School of Computing
)

[year] => Array
(
[id] => 140617569303679
[name] => 2007
)

[type] => College
)

[2] => Array
(
[school] => Array
(
[id] => 107577089271805
[name] => University of Colombo
)

[type] => Graduate School
[with] => Array
(
[0] => Array
(
[id] => 705760115
[name] => Lasitha Benaragama
)

)

)

)

[gender] => male
[timezone] => 5.5
[locale] => en_US
[languages] => Array
(
[0] => Array
(
[id] => 104023029632921
[name] => Sinhala
)

[1] => Array
(
[id] => 106059522759137
[name] => English
)

)

[verified] => 1
[updated_time] => 2012-07-29T16:34:22+0000
)


now i want to get fields

*email
*birth day


and some other permission requesting fields .

so i am doing

$this->co_config_items = $this->ci->config->item('connections_config');
$fbconfig = array(
'appId' => $this->co_config_items['fb_appid'],
'secret' => $this->co_config_items['fb_secret'],
'cookie' => true,
'domain' => $this->ci->config->item('domain')
);

$this->ci->load->library('3rd/connectors/facebook', $fbconfig);

$facebook = new Facebook($fbconfig);
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

$session = $facebook->getSession();

$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
));

if(!$session){
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'

));

echo "<script type='text/javascript'>window.location = '$url';</script>";
}
else{
//echo "<script type='text/javascript'>window.opener.location.href='" . base_url() . "home/auth/get_user_facebook_details_list" . "'; self.close();
//</script>";
try {

$me = $facebook->api('/me');

$this->ci->session->set_flashdata ( 'user_details',$me);
$url=base_url()."home/auth/user_contact_information";
echo "<script type='text/javascript'>window.opener.location.href = '$url';self.close();</script>";

} catch (FacebookApiException $e) {
$this->ci->message->set_error(array('An errror occured , please try again'));
}
}


i am trying to ask permissions by

$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
)):


but it is not asking for any permission , the default array is always printing . why is that . please help . thanks in advance

Answer

i was using the version 2 of PHP sdk , now i am using version 3 , now it is working , thank you very much . i saw some changes in two versions in this function

version 2

  public function getLoginUrl($params=array()) {
        $currentUrl = $this->getCurrentUrl();
        return $this->getUrl(
                'www',
                'login.php',
                array_merge(array(
                    'api_key' => $this->getAppId(),
                    'cancel_url' => $currentUrl,
                    'display' => 'page',
                    'fbconnect' => 1,
                    'next' => $currentUrl,
                    'return_session' => 1,
                    'session_version' => 3,
                    'v' => '1.0',
                        ), $params)
        );
    }

version 3

 public function getLoginUrl($params=array()) {
    $this->establishCSRFTokenState();
    $currentUrl = $this->getCurrentUrl();

    // if 'scope' is passed as an array, convert to comma separated list
    $scopeParams = isset($params['scope']) ? $params['scope'] : null;
    if ($scopeParams && is_array($scopeParams)) {
      $params['scope'] = implode(',', $scopeParams);
    }

    return $this->getUrl(
      'www',
      'dialog/oauth',
      array_merge(array(
                    'client_id' => $this->getAppId(),
                    'redirect_uri' => $currentUrl, // possibly overwritten
                    'state' => $this->state),
                  $params));
  }

in version 3 this i can see this additional code

// if 'scope' is passed as an array, convert to comma separated list
    $scopeParams = isset($params['scope']) ? $params['scope'] : null;
    if ($scopeParams && is_array($scopeParams)) {
      $params['scope'] = implode(',', $scopeParams);
    }

in version 2 , there is nothing called scope .and also

in version 2

return $this->getUrl(
                    'www',
                    'login.php',

in version 3

return $this->getUrl(
          'www',
          'dialog/oauth',