How to get email address on facebook API - php

I'm developing a Facebook app and downloaded facebook-php-sdk-master file from facebook. It is working great it is just i need the email address of the people who use our app.. and it does not return the email address... i made some research and a youtube video said to request it on getLoginUrl
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array('email'=>'email'));
}
but still doest return the email address. Please help. Thanks!

That's not correct. You're requesting the permission to read the user's e-mail address. Any permission requests should be made with the scope field, like so:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email'
));
The above code will create the URL. Now, to direct users to this URL, you can use the following code:
Login
When a user clicks on the above link, they'll be asked to supply the permission for reading their emails.
If you want additional permissions, you can request it the same way. Just add them to the scope field as comma-separated values. The full list of permissions, including defaults, can be found in the Facebook Developers documentation.

Related

Scope parameter is ignored by facebook login

I am trying to use Facebooks PHP SDK to login to my page. However, Facebook ignores the permissions given as an array in getLoginUrl()
$loginUrl = $helper->getLoginUrl(
['publish_actions']
);
The login url looks like:
https://www.facebook.com/v2.3/dialog/oauth?client_id=206265226055767&redirect_uri=http://host.tld/remote/gateway.php&state=67096befe627aa603cb086da681626c1&sdk=php-sdk-4.0.23&scope=publish_actions
However, Facebook still ask for my public_profile and my email
I have administrator privileges
The app is still in development mode
My Question is why Facebook ignores the scope parameter on the oauth login dialog.
Try with this code:
$params = array(
'scope' => 'publish_actions',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
Not sure if it works the same way with the PHP SDK 4.x though, but you can try this:
$scope = array('publish_actions');
$loginUrl = $helper->getLoginUrl($scope);
...from the well upvoted answer in this thread: Specify app scopes in php facebook sdk 4.0.0 or greater
Of course you can also write it like this:
$loginUrl = $helper->getLoginUrl(array('publish_actions'));
Not sure if it helps in your case, the literal array definition should definitely work in PHP...but you never know ;)
It's not being ignored
Initially Facebook shows a dialog so that the user can approve permissions to app, like profile, email, friends etc. What you're experiencing is specific to the publishing permissions like publish_actions.
Publishing permissions prompt a second dialog for the user to approve such permissions, which you'll get right after you take action about the permission request in the 1st dialog.
So,don't worry, it's not you, it's them.
Straight from the Facebook Permissions doc - https://developers.facebook.com/docs/facebook-login/permissions/v2.4#optimizing
Hoping that helped.

Can we get email ID from Twitter oauth API?

How to get email id of the user who accepted my Twitter application?
I have gone through lot of forums. But they have mentioned, it is not possible. Also those posts are older than a year. May I know whether it is possible to get the user email id through twitter API using PHP?
I am getting Twitter user details using the following URL:
https://api.twitter.com/1.1/account/verify_credentials.json
This is now possible by filling out a form to request elevated permissions:
Go to https://support.twitter.com/forms/platform
Select "I need access to special permissions"
Enter Application Name and ID. These can be obtained via https://apps.twitter.com/ -- the application ID is the numeric part in the browser's address bar after you click your app.
Permissions Request: "Email address"
Submit & wait for response
After your request is granted, an addition permission setting is added in your twitter app's "Permission" section. Go to "Additional Permissions" and just tick the checkbox for "Request email addresses from users".
Note that user e-mail address has to be verified, otherwise Twitter refuses to provide it. (See include_email parameter description in elevated permissions doc page.)
Now you can fetch user email address from twitter API and it's a lot much easier. Just follow these steps...
Goto Twitter Apps
Click on 'Create New App'
Fill all required credentials and click on 'Create your Twitter application'
Now click on 'Permissions' tab -> check 'Request email addresses from users' field and click on 'Update Settings'. (check given picture)
Now in your PHP code, set all app details and add this code!
$params = array('include_email' => 'true', 'include_entities' => 'false', 'skip_status' => 'true');
$data = $connection->get('account/verify_credentials', $params); // get the data
// getting twitter user profile details
$twt_id = $data->id; //twitter user id
$twt_email = $data->email; //twitter user email
All Done.
Hope it help you, good luck. ;)
It is now possible to retrieve a user's email address from Twitter (if the user permits it, of course).
You'll need to apply to have your application white-listed to receive xAuth.
Check here for more info:
https://dev.twitter.com/rest/reference/get/account/verify_credentials
Yes you can get email address.This is now possible by filling out a some permission.
1.Go to this Link: https://apps.twitter.com/
2.After go to permission tab inside your created Application
3.Select Addition permission checkbox in your APP.
4.After successfully update setting you can get email address from Twitter.
If in some case you can not getting email address then please check your Oauth Api request url.
Your request url should be this type :https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true
It is not possible to get user's email address from twitter. You can see it here. You can open a form page on your callback page and get user's email address on that page. You can refer here for example usage
Here is how I have done this in ASP.Net using linqtoTwitter library
http://www.bigbrainintelligence.com/Post/get-users-email-address-from-twitter-oauth-ap
// call verify credentials api
var twitterCtx = new TwitterContext(authTwitter);
var verifyResponse =
await
(from acct in twitterCtx.Account
where (acct.Type == AccountType.VerifyCredentials) && (acct.IncludeEmail == true)
select acct)
.SingleOrDefaultAsync();
if (verifyResponse != null && verifyResponse.User != null)
{
User twitterUser = verifyResponse.User;
//assign email to existing authentication object
loginInfo.Email = twitterUser.Email;
}

Facebook API missing fields on user profile

I am working on FB application using the PHP SDK.
The implementation seems to be correct and everything seems fine.
The problem I am facing is that some users profiles do not contain an email address. That is wierd because the application asks for this permissions specifically.
Code for this action:
// 1. If we cant get the user, log him in and request permissions if he has deleted/deauthorized or new to app
if (!$fbCurrentUserID){
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,user_birthday',
));
$this->abortToUrl($loginUrl);
}
try {
$user_profile = $facebook->api('/me');
if (!is_array($user_profile) || empty($user_profile['email'])) {
if ($log = $this->getLog()) {
$log->log('Facebook login no valid userprofile', Zend_Log::ALERT, array('user_profile' => $user_profile));
}
$this->abortToUrl($landingPage_url);
}
// 2.a User has provided data go on...
} catch (FacebookApiException $e) {
// 2.b Something bad happened. Worst case scenario so go to start
if ($log = $this->getLog()) {
$log->log($e->getMessage(), Zend_Log::CRIT);
}
// If he declined or anything went wrong go to LP
$this->abortToUrl($landingPage_url);
}
So I get quite a few logs with the user array not containing the email address field.
Am I missing something? I cannot reproduce this with test accounts or my account when I set email to be visible only to me.
Not every user has an email address set with FB, since you can also register using your mobile.

enable user_birthday permission facebook birthday in php

I m making a facebook app to display the birthday of a user.........
<?
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id)
{
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try
{
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
// Birth Date to be printed here........
echo "DOB: ".$user_profile['birthday'];
}
catch(FacebookApiException $e)
{
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}
else
{
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
Now , the problem is This prints the name as expected but prints nothing in place on date of birth.......When i referred further, I heard about the term facebook permissions (extended).....
and from the following link
http://developers.facebook.com/docs/reference/api/user/
I found that we need to enable permissions users_birthday or friends_birthday.
Now, How do we enable it in php code ??...Please help with samples.......
I m sorry if this question seems silly. I m just a new comer in facebook app developing and also a beginner in php.......
I tried the other examples in stackoverflow.com and it didn't help me much......
I see no indication you've read the Permission or Authentication documentation; you're not asking the user for permission to access their birthday.
Add user_birthday to the scope when sending the user to the Auth Dialog and you'll be able to access their birthday if they approve the permissions.
The permissions you configure in the App Settings only apply to (deprecated) Authenticated Referrals or the App Center login screen, your app needs to explicitly ask for the permissions in all other login flows
You have to ask Facebook to approve your Application. After facebook review it, you are able to use Extended Permissions.
According with this Documentation, if your app asks for more than than public_profile, email and user_friends, it will require review by Facebook before your app can be used by people other than the app's developers. 1
I had the same issue than you. I thought it was wrong coded, everything. Then I just saw this on facebook v2.0 Documentation.

How do you get the email using facebook api?

I'm using this code but I can't figure out how to get the email from the user. I know you must get special permission from the user but I don't know how.
I can get the public data from the user but that's not what I even need. The only thing I need is the email.
I guess it's something with this line:
$me = $facebook->api('/me');
I have read the documentation but I still don't know how I can get the email. How do I get the email from the user loggin in on my website with the facebook api?
You first need to request an extended permission "Email" from the user. You can do this using FB JS SDK by calling FB.login(), or PHP FB SDK by redirecting the browser by calling getLoginUrl().
FB JS SDK: FB.login method
FB.login(function(response) {
if (response.session) {
if (response.perms) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'email'});
FB PHP SDK method:
$login_url = $Facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email'
));
echo '<script type="text/javascript">top.location.href = "'.$login_url.'";</script>';
exit();
Once you have the required permission, you can then call this in your PHP:
$me = $facebook->api('/me?fields=email');

Categories