Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API, how to get users email?

I'm using the Graph API, but I can't figure out how to get a logged-in users email address.

The intro to Graph states "The Graph API can provide access to all of the basic account registration data you would typically request in a sign-up form for your site, including name, email address, profile picture, and birthday"

All well and good, but how do I access that info?

This is what I have so far:

$json = $facebook->api('/me');  $first = $json['first_name']; // gets first name $last = $json['last_name']; 
like image 450
kylex Avatar asked Aug 31 '10 17:08

kylex


2 Answers

The only way to get the users e-mail address is to request extended permissions on the email field. The user must allow you to see this and you cannot get the e-mail addresses of the user's friends.

http://developers.facebook.com/docs/authentication/permissions

You can do this if you are using Facebook connect by passing scope=email in the get string of your call to the Auth Dialog.

I'd recommend using an SDK instead of file_get_contents as it makes it far easier to perform the Oauth authentication.

like image 133
Gazler Avatar answered Sep 28 '22 06:09

Gazler


// Facebook SDK v5 for PHP // https://developers.facebook.com/docs/php/gettingstarted/5.0.0  $fb = new Facebook\Facebook([   'app_id' => '{app-id}',   'app_secret' => '{app-secret}',   'default_graph_version' => 'v2.4', ]);  $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); $response = $fb->get('/me?locale=en_US&fields=name,email'); $userNode = $response->getGraphUser(); var_dump(     $userNode->getField('email'), $userNode['email'] ); 
like image 39
masakielastic Avatar answered Sep 28 '22 06:09

masakielastic