Facebook login api get email
To get the email address of a user who has logged in using the Facebook Login API, you need to make a request to the /me
endpoint and include the email
permission in your authorization request.
Here are the steps:
- Register your app: First, you need to register your app on Facebook Developer Platform. Go to Facebook for Developers and create a new app.
- Get an App ID and App Secret: Once you've created your app, you'll get an App ID and App Secret. You'll need these to authenticate your app.
- Add the email permission: In the Facebook Developer Platform, go to your app's settings and add the
email
permission to the list of permissions your app is allowed to request. - Make an authorization request: When a user logs in to your app using the Facebook Login API, you'll need to make an authorization request to Facebook. You can do this by sending a GET request to
https://www.facebook.com/v3.3/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URI&scope=email
. - Get the access token: After the user grants permission, Facebook will redirect the user back to your app with an authorization code. You can then exchange this code for an access token by sending a POST request to
https://graph.facebook.com/v3.3/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URI&client_secret=YOUR_APP_SECRET&code=AUTHORIZATION_CODE
. - Make a request to the /me endpoint: Once you have the access token, you can use it to make a request to the
/me
endpoint to get the user's profile information, including their email address. You can do this by sending a GET request tohttps://graph.facebook.com/v3.3/me?access_token=ACCESS_TOKEN
.
Here's an example of what the request might look like in JavaScript:
const facebookApiUrl = 'https://graph.facebook.com/v3.3';
const appId = 'YOUR_APP_ID';
const appSecret = 'YOUR_APP_SECRET';
const redirectUri = 'YOUR_REDIRECT_URI';
const authorizationCode = 'AUTHORIZATION_CODE';
// Exchange authorization code for access token
fetch(`${facebookApiUrl}/oauth/access_token?client_id=${appId}&redirect_uri=${redirectUri}&client_secret=${appSecret}&code=${authorizationCode}`)
.then(response => response.json())
.then(data => {
const accessToken = data.access_token;
// Make request to /me endpoint to get user's profile information
fetch(`${facebookApiUrl}/me?access_token=${accessToken}`)
.then(response => response.json())
.then(data => {
const email = data.email;
console.log(email);
});
});
Note that the email
permission is only available for users who have granted permission for your app to access their email address. If a user has not granted this permission, you will not be able to get their email address.