Facebook Graph API, how to get users email?
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.
how to get user email in facebook graph api
You have to authorize users with the email
permission:
$dialog_url = 'http://www.facebook.com/dialog/oauth?client_id=' . FACEBOOK_APP_ID . '&redirect_uri=' . $callback_url . '&scope=email';
Source: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
In Facebook Graph API, what are the API calls to get a user's email address
https://graph.facebook.com/me?fields=name,email&access_token=[User's access token]
It´s called "Declarative Fields" and came with v2.4 of the Graph API: https://developers.facebook.com/docs/apps/changelog#v2_4
API Explorer test: https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Dname%2Cemail
Of course you need to use a User Token that includes the email
permission/scope. Debug your Token here: https://developers.facebook.com/tools/debug/accesstoken/
FB API GRAPH: why I can't catch the user email?
https://developers.facebook.com/docs/reference/javascript/FB.login/v3.2
Ask for the email
permission in the login process:
FB.login((response) => {
// handle the response
}, {scope: 'email'});
Also, you need to ask for the fields you want to get:
FB.api('/me', {fields: 'name,email'}, (response) => {
console.log(response.name + ', ' + response.email);
});
Make sure the user even has an Email, it´s not required. And make sure you actually get asked for the email
permission in the login popup.
Side Note: I would just use console.log(response)
, so you can see the whole object instead of some undefined
values.
In Graph Facebook API, how can I get the user's id given users's email?
Accordingly with the documentation, you just need to call the graph apis endpoint adding the user email address at the end of the url
GET /{email_address} HTTP/1.1
Host: graph.facebook.com
The Workplace integration needs Read user email addresses permissions, otherwise you will not be able to perform the call.
How to get users email and fd id using facebook login with graph api
The same way you access name. i.e
response.email
and response.id
but to fetch email address you must have the permission to access it.
Add scope="email"
to your FB Login Button.
[EDIT]
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
testAPI();
} else {
// cancelled
}
}, { scope: 'email' });
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.' + ' Email: ' + response.email + ' Facebook ID: ' + response.id);
});
}
Here's a good tutorial for beginners: http://www.loginworks.com/technical-blogs/404-working-with-facebook-javascript-sdk
How to get the email address of a Facebook user using the graph API?
You are using outdated method. You can use the code below to retrieve the email.
You can find more information about Facebook Connect from : http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-x-x-which-uses-graph-api/
$app_id = "xxxxxxxxx";
$app_secret = "xxxxxxxxxx";
$site_url = "xxxxxxxxxxxxx";
include_once "src/facebook.php";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
In Facebook Graph API, what are the API calls to get a user's email address and gender?
Have a look at
- https://developers.facebook.com/docs/graph-api/reference/v2.2/user#fields
- https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#fields
to get an overview what fields and edges you can query for the user object. In your case, that should be
/me?fields=id,gender,email
or
new FacebookRequest(
$session, 'GET', '/me?fields=id,gender,email'
)
Be aware that you'll need the user_email
permission to be able to request the email
field.
To get specific fields, use the getProperty()
method as described at https://developers.facebook.com/docs/php/GraphObject/#getproperty
echo $user_profile->getProperty('gender');
Related Topics
What's the Net::Err_Http2_Protocol_Error About
How to Iterate Over the Results in a MySQLi Result Set
PHP Json_Encode Class Private Members
How to Show Ajax Requests in Url
Check If a User/Guest Has Purchased Specific Products in Woocommerce
Creating a Thumbnail from an Uploaded Image
Redefine Built in PHP Functions
How to Fix Trying to Access Array Offset on Value of Type Null Error
How to Rely on PHP PHP.Ini Precision Workaround For Floating Point Issue
How to Include Code into a PHP Class
What Is the Best Collation to Use For MySQL With PHP
Is Possible to Keep Session Even After the Browser Is Closed
PHP Script to Loop Through All of the Files in a Directory
How to Remove All Specific Characters At the End of a String in PHP
Generating (Pseudo)Random Alpha-numeric Strings