Software & Finance





Facebook Apps - How to check permission to read user email address using Graph API





First you need to have PHP SDK to start with. You can download the PHP SDK for facebook at the following location:

https://github.com/facebook/php-sdk

 

 

Here is the source code that checks and gets to access the email address through Graph API:

    
    include_once 'facebook.php';

	$facebook = new Facebook(array(
		'appId' => 'YOUR_APP_ID',
		'secret' => 'YOUR_SECRET_KEY',
		'cookie' => true,
		'domain' => 'www.softwareandfinance.com'
	));

	$app_name = "Broadcast to Friends";
	$app_url = "http://apps.facebook.com/your_app_name/";

	$user_id = $facebook->getUser();

	if (!$user_id) {	
		$url = $facebook->getLoginUrl(array(
		'canvas' => 1,
		'fbconnect' => 0
		
		));

	} else 
	{
		try {
			$permissions = $facebook->api("/me/permissions");	
            if(! array_key_exists('email', $permissions['data'][0]) ) {				
                header( "Location: " . $facebook->getLoginUrl(array(
                        "scope" => "email",
                )) );
            }
            $uid = $facebook->getUser();
            $me = $facebook->api('/me?fields=name,email');
            echo "Hello " . $me['name'] . "
" . $me['email'] . "
"; } catch (FacebookApiException $e) { echo "Error: Unable to read email address"; } }