Javascript facebook login get email [duplicate] - javascript

This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 2 years ago.
I am trying to allow users to register and login on my site using facebook, the following code works and I can see the users name and ID in the response.
However, I am unsure how to get the email address of the user. I have checked that my app is live on facebook developers dashboard and the permissions and features section includes email.
FB.getLoginStatus(function(response){
if(response.status === 'connected'){
FB.api('/me', function(response){
console.log(response);
});
}
});
window.fbAsyncInit = function(){
FB.init({
appId:'326746698293638',
cookie:true,
xfbml:true,
version:'v7.0'
});
};
(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'));

Replace
FB.api('/me', function(response) { } )
by
FB.api('/me?fields=id,email,name', function(response) { } )
and make sure you are using scope email on the login function, like this
FB.login(function(response) {
// handle the response
}, {scope: 'email,user_likes'});
See docs for more details

Related

Facebook Graph API with Angularjs

I'm trying to initialize Facebook's graph API in my angularjs app but having issues getting it started.
The angularjs framework page on Facebook developers site is very short and quite uninformative to an angularjs beginner like me, what I've got so far I've pieced together through the javascript SDK quick start and various posts found on here.
The end goal is to GET posts from several of my public pages and display them as a feed on my webpage.
I will display the feed info in individual cards that when clicked will take the user to that post on Facebook. So no actual changes will be made by the user on my webpage.
Most information I've read regarding this has started with having the user login by clicking a button, but is that necessary for my needs?
I tested it locally and I received the error regarding it having to be HTTPS, so I've now put it on a server with HTTPS, but I'm not getting any responses now.
A few more questions:
Is a user required to log in just to view public Facebook posts? Is the app ID enough or do I need to include my token somewhere?
controller.js
angular.module('myApp')
.controller('FacebookFeedCtrl', FacebookFeedCtrl)
function FacebookFeedCtrl($scope) {
window.fbAsyncInit = function() {
FB.init({
appId : '111111111111111',
xfbml : true,
version : 'v9.0'
});
FB.AppEvents.logPageView();
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
console.log(response);
});
FB.api('/me','GET',{"fields":"id,name"},function(response) {
console.log(response);
});
};
(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'));
$scope.getMyLastName = function() {
facebookService.getMyLastName()
.then(function(response) {
$scope.last_name = response.last_name;
console.log("this works")
}
);
};
}
service.js
angular.module('myApp')
.factory('facebookService', function($q) {
return {
getMyLastName: function() {
var deferred = $q.defer();
FB.api('/me', {
fields: 'last_name'
}, function(response) {
if (!response || response.error) {
deferred.reject('Error occured');
} else {
deferred.resolve(response);
}
});
return deferred.promise;
}
}
});
html
<div ng-controller="FacebookFeedCtrl">
<h1>testing: {{last_name}}</h1>
</div>

Facebook login: how to get email and profile picture in custom size?

I've a FB.login method that should get the email of the users from facebook but it always give me
undefined
I've got the code from facebook developers page and also with their test user that facebook provide I can't get the email.
Another thing that I can't do is to get the profile picture in different size, the urls that I've found searching where there are height and width params won't work with my code.
API version is v3.2
Here is the code that after connection get the information from profile
(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/it_IT/sdk.js#xfbml=1&version=v3.2&appId=405257833547582&autoLogAppEvents=1';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', {fields: 'last_name,first_name,email'}, function(response) {
console.log('Successful login for: ' + response.name);
$.post("Oauth.php", //Required URL of the page on server
{ // Data Sending With Request To Server
name:response.first_name,
surname:response.last_name,
user_id:response.id,
type_connection:'facebook',
email:response.email
},
function(response,status){ // Required Callback Function
alert("*----Received Data----*nnResponse : " + response+"nnStatus : " + status);//"response" receives - whatever written in echo of above PHP script.
$("#form")[0].reset();
});
});
}

How to check User facebook page like or not in v3.1

I have crated app on this link https://developers.facebook.com/ My app version is v3.1. I have use facebook api only scripting.
I want to check current user is like facebook page or not,If user is not like facebook page then request to like page,if user is liked then content are showing.
window.fbAsyncInit = function () {
FB.init({
appId:'xxx',
xfbml: true,
cookie: true,
status: true,
secret: 'xxx',
version: 'v3.1'
});
FB.Event.subscribe('edge.create',
function (href, widget) {
//alert('Ti piace ' + href);
//document.getElementById('fb-like').style.display = "none";
//var x = document.getElementsByTagName("button")[0].getAttribute("title");
// alert(x);
});
FB.api({ method: 'pages.isFan', page_id:'xxx' }, function (resp) {
console.log(resp);
if (resp) {
alert('You like it.');
} else {
alert("You don't like it.");
}
});
};
(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'));
Error are occurred like this. I am tired please help me this problems.
erro_code:"104" erro_msg:"Incorrect signature(104),request_args:Arrar(7),error_subcode: "0"
Gating content behind likes is not allowed since many years.
The edge.create event has been removed. It would only allow to detect new likes anyway, not existing ones.
"pages.isFan" is very old, it would be /me/likes instead to get the Pages a user likes. And that only works if the user authorizes your App with the user_likes permission. You will not get that permission approved in the review process for like gating or incentivizing users to like your Page in any way.
TL;DR: Not possible and not allowed.

FB.login not asking for permissions [duplicate]

This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 7 years ago.
I am trying to integrate FB login with my html website. I am using FB Javascript SDK version 2.5. The pop-up opens and ask the users for the userid and password but donnot ask for permissions like email, public profile, location etc although i have given the required permissions in the scope param. Below is my code.
HTML code:
<div id="btnFBLogin"> <img style="width: 35%"
src="assets/img/icons/facebookloginbutton.png" />
</div>
Initializing FB Javascript SDK and gets initialized on document load :
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXX',
xfbml : true,
oauth : true,
version : 'v2.5'
});
};
//Load SDK
(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'));
Calling FB.login api:
$('#btnFBLogin').on('click',function(){
FB.login(function(response) {
console.log(JSON.stringify(response));
if (response.status === 'connected') {
// Logged into your app and Facebook.
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
});
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}, {scope: 'public_profile,email'});
});
The response object returned after login by FB.api('/me', function(response) gives only full name of the user logged in. It doesn't give me the email id although i have send it in the scope parameter.
you need to pass requested fields in url like
FB.api('/me?fields=id,first_name,last_name,friends,email',function(response)

Facebook Canvas App cant login to Developer

when Try to login Facebook Canvas App only Developer can't login to App Show following Error
Invalid Scopes: read_friendlists. This message is only shown to
developers. Users of your app will ignore these permissions if
present. Please read the documentation for valid permissions at:
https://developers.facebook.com/docs/facebook-login/permissions
I use Following code inside index.php file and bottom of the code
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo APP_ID;?>',
xfbml : true,
version : 'v2.3'
});
function onLogin(response) {
if (response.status == 'connected') {
FB.api('/me?fields=first_name', function(data) {
var welcomeBlock = document.getElementById('fb-welcome');
welcomeBlock.innerHTML = 'Hello, ' + data.first_name + '!';
});
}
}
FB.getLoginStatus(function(response) {
// Check login status on load, and if the user is
// already logged in, go directly to the welcome message.
if (response.status == 'connected') {
onLogin(response);
} else {
// Otherwise, show Login dialog first.
FB.login(function(response) {
onLogin(response);
}, {scope: 'user_friends,email,read_friendlists'});
}
});
FB.Canvas.setAutoGrow();
};
(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'));
This happen on 6th of may 2015, before that it works fine..
Check this link and check your api version.
https://developers.facebook.com/docs/facebook-login/permissions/v2.3
As per this link
"If you want to access a person's friends who also use your app, you should use the user_friends permission.
This permission will also not help you invite a person's friends to use your app. To learn more about how to invite friends to an app, please see our FAQs.
This permission also does not give the list of friends who are part of a friendlist. It only gives access to the names of the lists.
This permission was called read_friendlists before v2.3.
Review
If your app requests this permission Facebook will have to review how your app uses it."

Categories

Resources