Online Forum To Support Coders With Technical Assistance - Read Write Execute

Fetch facebook profile data using javascript

Ankit Agrawal
Ankit Agrawal
Published on July 10, 2018 · 1 min read

fetch Facebook profile data

Facebook is the most popular social networking site. Facebook provides graph api to get dataย  . We can fetch Facebook profile data using JavaScript also.

<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : 'Your Facebook App Id',
cookie : true,
xfbml : true,
version : 'v2.8'
});
};

(function(d, s, id)
{
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function fbLoginFunction()
{
FB.login(function(response) {
if (response.authResponse)
{
FB.api('/me', 'get', { fields: 'id,name,gender,hometown,email' }, function(response) {
console.log(response);
});
} else {
alert("Login attempt failed!");
}
}, { scope: 'email,user_birthday,user_location,user_hometown,public_profile' });
}
</script>
<a href="javascript:void(0);" onclick="fbLoginFunction();" class="btn btn-primary btn-xl rounded-pill mt-5">Login</a>
Was this article helpful?
Ankit Agrawal

Ankit Agrawal

Author & Senior Engineer at CodeExecute

Writing practical engineering guides, debugging real-world bottlenecks, and creating free tools for developer productivity.

← Previous Article Create custom helper in laravel 5 Next Article → How to get user agent in php