How to get users email and id fd using facebook login with api schedule

This is my first time working with the facebook API. From there, the documentation I found, we can get the name below console.log (response.name); But how can I get email and fbid?

Thanks Enamul

<?php ?> <div id="fb-root"></div> <script> // Additional JS functions here window.fbAsyncInit = function() { FB.init({ appId : 'f', // App ID channelUrl : '//localhost/practices/phpTest/fblogin/login.php/channel.html', // Channel File status : true, // check login status cookie : true, // enable cookies to allow the server to access the session xfbml : true // parse XFBML }); FB.getLoginStatus(function(response) { if (response.status === 'connected') { // connected } else if (response.status === 'not_authorized') { // not_authorized login(); } else { // not_logged_in login(); } }); // Additional init code here }; function login() { FB.login(function(response) { if (response.authResponse) { // connected testAPI(); } else { // cancelled } }); } function testAPI() { console.log('Welcome! Fetching your information.... '); FB.api('/me', function(response) { console.log('Good to see you, ' + response.name + '.'); }); } // Load the SDK Asynchronously (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all.js"; ref.parentNode.insertBefore(js, ref); }(document)); </script> <?php ?> 
+4
source share
2 answers

In the same way, you get access to the name. i.e

response.email and response.id

but in order to receive an email address you must have 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 primer for beginners: http://www.loginworks.com/technical-blogs/404-working-with-facebook-javascript-sdk

+4
source

In addition to adding an area = "email" to the button, you need to specify which fields will be returned.

 <fb:login-button scope="public_profile, email" onlogin="checkLoginState();" data-auto-logout-link="true" data-size="large"> 

 function testAPI() { FB.api('/me', {fields: 'name, email'}, function(response) { console.log( response ); console.log( response.email ); }); } 

For a complete code structure, take a look at the facebook documentation .

+4
source

All Articles