Getting Facebook user info after success login in Ionic Cordova Facebook 4 - facebook

I'm trying to get user informations after a success login using IOnic 2 and cordova-plugin-facebook4. The first part showing the access Token is correctly showing up, while after that neither success nor fail are showing anything. I'm trying to simply show an alert with user name gathered from Facebook.
let params = ["public_profile", "email"];
this.facebook.login(params).then(
function(response) {
alert("Login Response :" + JSON.stringify(response.status));
let authId = response.authResponse.userID;
if (response.status == "connected") {
this.facebook.api( authId + "/?fields=id,email,first_name,last_name,gender,age_range",
['public_profile', 'email'],
function onSuccess (response) {
alert(JSON.stringify(response.email));
},
function onError (error) {
alert("Failed: " + error);
})
} else {
alert("Not connected: " + response.status);
}
},
function(response) {
alert("Other Response : " + JSON.stringify(response))
});
}
All I'm getting is an alert saying 'connected', no alert for success neither for fail ..

Related

Facebook getLoginStatus gets status: "unknown" even when logged into Facebook

I am trying to ascertain whether a user is logged into Facebook using the JS SDK.
The call to FB.getLoginStatus always returns status="unknown", even when I know
I am logged into Facebook - literally, I have my Facebook home page open in the
next browser tab.
I am loading the API like this:
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>
And in my JS code:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
function statusChangeCallback( response )
{
console.log("login status change callback: ", response);
if (response.status === 'connected')
{
testAPI();
}
else if (response.status === 'not_authorized')
{
// Not authorized, so show the login button
show_block( ".loginScreen" );
}
else
{
// nothing doing
show_block( ".loginScreen" );
}
} // end statusChangeCallback()
window.fbAsyncInit = function() {
FB.init({
appId : <my app id>,
cookie : true,
xfbml : true,
status : true,
version : 'v12.0'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
My console window shows:
login status change callback: {authResponse: null, status: 'unknown'}
Why would I be getting a "unknown" status here? I have Facebook open in the next browser tab, so I know that I'm logged in.

Connecting Facebook page and Facebook application

Goal : Connecting an application with existing Facebook page.
Why ? I want to allow user to use their page as bot. Facebook app is a bot.
Reason : User wants a bot for Facebook page but does not allow admin permissions.
What I've tried :
Get all users pages :
if ( typeof (response.authResponse) !== 'undefined')
{
token = response.authResponse.accessToken;
$.ajax({
url: "api/FB/GetCode?token=" + token,
}).done(function (data) {
var mySelect = $('.dropdown');
mySelect.empty();
$.each(data.accounts.data, function (val, text) {
mySelect.append(
$('<option></option>').val(text.access_token).html(text.name)
);
});
mySelect.show();
$('.connectWithBot').show();
});
} else {
//do the login logic
}
Then when user has chosen page :
FB.api(
'pageId/subscribed_apps?access_token=' +
$('.dropdown').find(":selected").val(),
'post',
function (response) {
console.log(response);
if (response && !response.error) {
console.log(response);
}
});
So when one page instance is connected with one application, this not gonna work anymore. Its okey that way.
Is it possible to get this work ? Or maybe you guys have a better idea ?

Cordova: Can't able to get user information, it shows 'malformed access token'

I am about to finish my cordova application where i am using the facebook social login. I am trying to access the user information from the graph API that openfb.js is calling. My code snippet is this:
function login() {
openFB.login(
function (response) {
if (response.status === 'connected') {
alert(JSON.stringify(response));
FACEBOOK_TOKEN = response.authResponse.accessToken;
var message = "Connected to Facebook_____";
alert('Message is :' + message);
alert('Access token is :' + FACEBOOK_TOKEN);
getInfo(response.authResponse.accessToken);
} else {
var errorMessage = 'Facebook login failed: ' + response.error + "_____";
alert(errorMessage);
}
}, {scope: 'email,public_profile,user_birthday,user_location'});
}
function getInfo(access_token) {
openFB.api({
path: '/2.5/me?access_token=' + access_token,
success: function (data) {
alert(JSON.stringify(data));
var message = "Logged in with Facebook as " + data.name + "_____";
alert(message);
},
error: errorHandler
});
}
function errorHandler(error) {
var errorMessage = error.message;
alert('Error is : '+errorMessage);
}
Here in this case when i am getting the access token of the user but when i pass it to graph API that is in the get info function, then it shows me the error:
Error is : Malformed access token CAANoTLg2W..........
I am not able to debug the error ocuring here. Please help me resolve it.
Thank in advance for any response.

Facebook Graph API - Error on success?

I am using the Facebook Javascript SDK to upload a photo to the user's timeline. This is my call:
function post_photo_to_facebook() {
var file_name = 'https://my-image.url.com/image.jpg';
var access_token = FB.getAuthResponse()['accessToken'];
$.ajax({
type: "POST",
url: "https://graph.facebook.com/me/photos",
data: {
message: "Here is my message",
url: file_name,
access_token: access_token,
format: "json"
},
success: function(data){
alert("POST SUCCESSFUL");
},
error: function(data){
alert('Error');
console.log(data);
}
});
}
When in Chrome, I am receiving an Error back from this AJAX call, yet the statusText is "OK", and the image is being successfully uploaded to my timeline. I am just wondering what I am missing here - why is the error being called?
You should be using FB.api to upload images, rather than ajax POST due to CORS reasons.
So your code above would look like this:
var file_name = 'https://my-image.url.com/image.jpg';
FB.api('/me/photos', 'post', {
message:'Here is my message',
url:file_name
}, function(response){
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
and you use FB.init to set up your tokens etc.

Facebook login not responding to FB.login response

I am working on a android phonegap application with facebook integration.
FB.login(function(response) function has a response handler it is being called when
user click on fb button, but its not getting into the function response
or alert('test') every time. it gets into this script only when i hit
around 5 or 6 times. wat i need is to fetch the access token the very first time i logged into facebook. i have gone through a lot of links regarding this but
cant find an exact solution for this...
FB login callback function not responding if user is already logged in facebook
Even this question seems to be same but i cant figure it out my solution.
this is the code am working :
<div id="data">Hello Facebooktesters, loading ...</div>
<button onclick="login()">Login</button>
<button onclick="me()">Me</button>
<button onclick="logout()">Logout</button>
<button onclick="Post()">facebookWallPost</button>
<script type="text/javascript">
document.addEventListener('deviceready', function() {
try {
alert('Device is ready! Make sure you set your app_id below this alert.');
FB.init({
appId : "xxxxxxxxxxxxxxx",
nativeInterface : CDV.FB,
useCachedDialogs : false
});
document.getElementById('data').innerHTML = "FB init executed";
} catch (e) {
alert(e);
}
}, false);
function me() {
FB.api('/me/friends', {
fields : 'id, name, picture'
}, function(response) {
if (response.error) {
alert(JSON.stringify(response.error));
} else {
var data = document.getElementById('data');
fdata = response.data;
console.log("fdata: " + fdata);
response.data.forEach(function(item) {
var d = document.createElement('div');
d.innerHTML = "<img src="+item.picture+"/>" + item.name;
data.appendChild(d);
});
}
var friends = response.data;
console.log(friends.length);
for ( var k = 0; k < friends.length && k < 200; k++) {
var friend = friends[k];
var index = 1;
friendIDs[k] = friend.id;
//friendsInfo[k] = friend;
}
console.log("friendId's: " + friendIDs);
});
}
function login() {
FB.login(function(response) {
alert('test');
if (response.authResponse) {
alert('logged in');
var access_token = FB.getAuthResponse()['accessToken'];
alert(access_token);
window.location = "test.html"
} else {
alert('not logged in');
}
}, {
scope : "email"
});
}
function logout() {
alert('test');
FB.logout(function(response) {
alert('logged out');
//window.location.reload();
});
}
function Post(ele) {
var domain = 'http://192.168.0.46:8082/';
console.log('Debug 1');
var params = {
method: 'feed',
name: 'test - test',
link: domain+'test/test/showproddetails.action?product.productId=1',
picture: 'http://www.careersolutions.com/test.png',
caption: 'test',
description: 'test'
};
console.log(params);
FB.ui(params, function(obj) { console.log(obj);});
}
</script>
Thank you,
raj
You are missing semicolon in this line :
window.location = "test.html"
This question was posted a while ago but I would like to point out something. FB.login function opens a popup dialog asking if the user would like to log in to your app using Facebook. It is generally recommended that it be run after clicking a button as most browsers would block popup dialogs when a page loads. If the user had previously authorized your app then the popup dialog redirects back to your app and closes the dialog box (assuming your app is a website).
If what you intended to do was to check whether a user is logged in to your app upon page load, then it's recommended that you instead use FB.getLoginStatus method like so
function checkLoginState() {
FB.getLoginStatus(function(response) {
// Check response.status to see if user is logged in
alert(response.status);
});
}
Refer to this extensive documentation on facebook