How to use facebook sdk in angular 2

May 5th, 2017 . 3 minutes read
Blog featured image

What is Facebook SDK

Facebook SDK provides a way to use Facebook features, eg: – like the plugin to use like button and other social plugins. By using Facebook SDK we can also access user’s data after logging it by the user. It also provides the functionality of Facebook login on the website.

To use Facebook SDK you should have Facebook account and app on Facebook developer page.

To create an app on Facebook developer page, you have to go to https://developers.facebook.com/ this link and log in to your Facebook account and after that click on Add a New App button, that will be on left side of the page, it will just ask for app name and email id. App name can be any name that you want to associate with this App ID, so in this way, it is very easy to create an app on Facebook developer page. After creating an app on facebook developer page you will get an app id, using this app id you can use facebook SDK.

How to use angular 2 Facebook SDK:

To use Facebook SDK in your site first you need to include

This SDK in your file
To use Facebook SDK for login
Write declare const FB: any;
in any page where you have to use this SDK eg-(in login component.ts)
And then in constructor, initialize it by using app id that is provided by facebook developer page


and then for login, write login function of facebook SDK in the login function

Here this.me is a function that is used to get user information from Facebook

Here userid is id and accesstoken is token that we get in response of facebook login function and fields are the user data that we want from facebook api
And in result, we will get user’s data

To logout use this logout function of Facebook SDK



If you need to discuss any issue or query then ask in comment box. We are angular.js development expert company.

Share with friends if you enjoy the post.

Author: shilpa