Reputation: 7094
iam using openid for login in my site, and upon successful login, i could get the user's email and other parameters like first name.
But for facebook, iam using facebook connect (Graph api) and after successful login, iam getting an access token. But using this token, iam not able to fetch the user's email. I end up with an error saying either invalid token or invalid redirect url.
Upvotes: 0
Views: 537
Reputation: 1235
After you get your initial token, you must redirect the user to the login URL requesting the permissions you want, in this case, "email". The easiest way to implement this is using the PHP SDK provided by Facebook.
If the user grants the permissions, you'll be able to query the graph api to get the email address (along with other basic information.) Once you have a valid working token and the user has authorized your application, the email address will be included $user_data.
$facebook = new Facebook(array('appId'=>APP_ID, 'secret'=>APP_SECRET, 'cookie'=>true));
try{
$user_data = $facebook->api('/me');
} catch (Exception $e){
header('Location: ' . $facebook->getLoginUrl(array('perms'=>'email'));
die();
}
print $user_data['email'];
Be aware that the email address may be a proxy address and be much longer than a typical email address so allocate sufficient space in your storage system. For example, the address might look something like this: apps+2356396727.745.22667a3160x3d6wd2c1396c4efd7277b@proxymail.facebook.com
Upvotes: 2