Login with Facebook API V2

Sep 12th, 2014

Login with Facebook API V2

Share

In this blog, we will explain how to implement login with facebook using Javascript SDK v2.1.

window.fbAsyncInit = function() {
FB.init({
appId : ‘YOUR_APP_ID’,
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : ‘v2.1’ // use version 2.1
});
};
(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 = “//connect.facebook.net/en_US/sdk.js”;
fjs.parentNode.insertBefore(js, fjs);
}(document, ‘script’, ‘facebook-jssdk’));

By help of above code, we can load sdk and initialize required parameters.

We have created a function which will call facebook login API, fetch response and allows developer to write code to handle different success/failure cases.

function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
console.log(‘Welcome! Fetching your information…. ‘);
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api(‘/me?fields=id,name,friendlists’, function(response) {
user_email = response.email; //get user email
console.log(response);
alert(user_email);
// you can store this data into your database
});
} else {
//user hit cancel button
console.log(‘User cancelled login or did not fully authorize.’);
}
}, {
scope: ‘publish_stream,email,read_friendlists’
});
}

In the above example, we have passed a scope parameter, by which Facebook will identify and ask all required permissions to user.

Facebook by default gives access to email, public_profile and user_friends permission. If the developer wants to get other information, he has to get them reviewed from Facebook for the app using the App settings tab. Unless it is reviewed and approved by Facbook, this parameter will not work as expected.

In FB.api, we have passed query parameters. FB will only return these many values in response if passed.

Facebook has announced stable support for at least 2 years for API v2.

Below is the full working code.

<title>Facebook Login JavaScript Example</title>
<meta charset=”UTF-8″>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : ‘572516629527309’,
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : ‘v2.1’ // use version 2.1
});
};
(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 = “//connect.facebook.net/en_US/sdk.js”;
fjs.parentNode.insertBefore(js, fjs);
}(document, ‘script’, ‘facebook-jssdk’));
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
console.log(‘Welcome! Fetching your information…. ‘);
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api(‘/me?fields=id,name,friendlists’, function(response) {
user_email = response.email; //get user email
console.log(response);
alert(user_email);
// you can store this data into your database
});
} else {
//user hit cancel button
console.log(‘User cancelled login or did not fully authorize.’);
}
}, {
scope: ‘publish_stream,email,read_friendlists’
});
}
</script>
<a href=”#” onclick=”return fb_login();”>
<img src=”http://www.myclientpage.com/images/fb_login.gif”>
</a>
<div id=”status”></div>

Comments are closed.

Let's Discuss Your Project

Get free consultation and let us know your project idea to turn
it into an amazing digital product.

Let’s talk

NEWS & BLOG

Related Blogs

Why should you hire React Native developers for your project?

Hire Developer Jun 19th, 2023

Why should you hire React Native developers for your pr...

Read more
Explorando A 1xbet: Uma Análise Detalhada Da Plataforma Sobre Apostas Online

Hire Developer Feb 24th, 2023

Explorando A 1xbet: Uma Análise Detalhada Da Plataform...

Read more
Explorando A 1xbet: Uma Análise Detalhada Da Plataforma Sobre Apostas Online

Hire Developer Feb 23rd, 2023

Explorando A 1xbet: Uma Análise Detalhada Da Plataform...

Read more

INQUIRY

Let's get in touch

UNITED STATES

31236 Meadowview Square,
Delmar, DE 19940, USA

Sales: +1 667 771 6758

UNITED KINGDOM

13 Layton Road, Hounslow,
London, TW3 1YJ

Sales: +44 7404 607567

INDIA

2nd Floor, Sun Avenue One, Bhudarpura, Ayojan Nagar, Nr. Shyamal Cross Road, Ahmedabad, Gujarat-380006

Sales: +91 635-261-6164

For Project Inquiries

biolah

depo 25 bonus 25 to 5x

depo 25 bonus 25

depo 25 bonus 25

mndrmndr.com

bonusdeposit.net

https://www.greentourstanzania.com/wp-includes/customize/

https://temp1.novotest.biz/id/

depo 25 bonus 25

https://sumberjo-blitar.desa.id/images

https://sumberjo-blitar.desa.id/data

depo 25 bonus 25 to 5x

depo 25 bonus 25

https://www.greentourstanzania.com/wp-includes/js/product/

https://smpabbs.sch.id/gacor/100/

https://smpabbs.sch.id/gacor/bonus/

deposit 25 bonus 25

depo 25 bonus 25

bonus new member 100

https://ppdb.smk-kosgoro.sch.id/data/depo 25 bonus 25https://jesus.nouvellevie.com/wp-includes/images/Getoko.iddepo 25 bonus 25https://bonus-baru.s3.ap-southeast-1.amazonaws.com/link-daftar-slot-gacor.htmlhttps://bonus-baru.s3.ap-southeast-1.amazonaws.com/scatter-pink-paling-gacor.htmlhttps://worldlisteningproject.org/wp-includes/depo25bonus25/bonus new member 100depo 25 bonus 25