What is the error in the following code - facebook

Here is a function which is call when user click a button to attend a event on facebook, these following code get the login information from user and add there particular event to the user fb event to attending the event.
These code is giving me the syntax error, i does not Know what is the error. Please help its needy.
function gogingtoevent() {
FB.login(function(response) {
if (response.status == 'connected') {
FB.api('/816891388384961/attending',
function (response) {
if (response && !response.error) {
alert('Attending');
}else{
alert (JSON.stringify(response));
}
});
}else{
alert('Not Responding');
}
});
}

You need to do a POST request to this edge, and you need the rsvp_event permission.
It's all in the docs:
https://developers.facebook.com/docs/graph-api/reference/v2.3/event/attending#publish
https://developers.facebook.com/docs/facebook-login/permissions/v2.3#adding

Related

Facebook share callback, more data?

I'm using a simple Facebook share dialog:
FB.ui(
{
method: 'share',
href: 'http://example.com'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.'+JSON.stringify(response));
} else {
alert('Post was not published.'+linkToShare);
}
}
);
When I get a success callback, all it gives me is the post id. Is there a way to get more data?
What I really want to know is how the page was shared e.g. Only Me, Friends or Public
Is this data out there anywhere?
If you want to know more, you need to use OAuth to get information about the web app using user.
Example:
FB.api('/me', function(response) {
console.log(response);
});
EDIT: Here you can find out more about to get information from a single post

What to do after getting 'not_authorized' back from Facebook Connect login

After your get your response from getLoginStatus, is the idea to then call FB.login? Currently when I do that, the code stops.
Here's the code for when they hit the FB button to login, which correctly gives me the response...
function facebookConnectBtnLogin()
{
FB.getLoginStatus(function(response)
{
CL();
if (response.status === 'connected') {
logFacebookUserIn(response);
} else if (response.status === 'not_authorized') {
// alert('not_authorized...');
facebookLoginInit();
} else {
//alert('not logged into Facebook...');
facebookLoginInit();
}
},true);
}
And the code that deals with FB.login, which alerts 'here4', but not 'here5'...
function facebookLoginInit()
{
alert('here4');
FB.login
(
function(response)
{
alert('here5');
CL();
if(response.authResponse)
{
alert('here6'); logFacebookUserIn(response);
} else alert('not connected');
},
{ scope: "email" }
);
}
Do I have the right idea?
Thanks for your time and help.
You should never call FB.login in an asynchronous callback function, ONLY on direct user interaction (mouse click). Browsers block the FB.login popup if you don´t call it on user interaction.
With FB.getLoginStatus you just check if the user is authorized, if not: present a Login Button where the user can click to login. It´s not a good idea to show the login dialog right when the user enters your App anyway, tell him what the App is about first.

Emberjs: check if user is logged with facebook on init

I'm trying to use Facebook to log in an Ember application. The login action is working, but I can't find how to check if the user has already logged in when the app is initialized.
I'm calling the auth.authResponseChange event but it blocks the app and it never starts.
App.ApplicationController = Ember.Controller.extend({
fbUser: false,
init: function() {
this.checkFBUser()
},
checkFBUser: function() {
FB.Event.subscribe('auth.authResponseChange', function(response) {
// this is never triggered and the app does not init
console.log(response)
if (response.status === 'connected') {
console.log('Logged in')
}
else {
FB.login()
}
})
},
// this works
actions: {
loginFacebook: function() {
var self = this
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
self.set('fbUser', response)
})
}
})
}
}
})
How am I supposed to do this?
I haven't worked with FB lately, but I'm pretty sure the AuthResponeChange event is for being notified of a change in the AuthResponse status; in this instance you'll probably want to take a look at FB.getLoginStatus instead.
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus
checkFBUser: function()
{
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
I've found the source of the problem and a solution. I'm not sure this is the best solution so I'm open to other suggestions but if this can help other people, here is the thing:
The issue was that the Facebook library was not loaded yet, so the FB variable was undefined. For some reason if you try to do something with an undefined variable in Ember the app just won't work without even a warning. So the solution is to wait:
App.deferReadiness()
function checkFB() {
if (typeof FB !== 'undefined') {
APp.advanceReadiness()
}
else {
setTimeout(checkFB, 10);
}
}
setTimeout(checkFB, 10);
10 milliseconds is pretty short so the app is still initialized very quick, and in my tests the app starts after ~20 milliseconds. This still feels a bit hacky but in place of a better solution this will do for now.
As Aaron said you need to use fb.getLoginStatus to check if a user is already logged in. To make sure Facebook is loaded before you to make any calls you can use an initializer and defer the app until the SDK has loaded.
http://nerdyworm.com/blog/2013/04/03/ember-initializers/
Ember.Application.initializer({
name: 'init_fb',
initialize: function (container) {
window.App.deferReadiness();
Ember.$.getScript( '//connect.facebook.net/en_UK/all.js', function( ) {
FB.init( {
appId : 'xxxxxxxxxxxxx',
xfbml : true,
version : 'v2.0',
} );
window.App.advanceReadiness();
} );
}
});

User Skips Permission in Facebook Auth Dialogue Javascript SDK

I have an app in a page tab that uses the facebook javascript sdk. When a new user comes to the app, I get the expected "Log in with facebook" pop-up. I also have some extended permissions that I've put in the scope parameter of FB.Login. After the users logs in with facebook I see the expected extended permissions pop-up. The only problem is if the user skips the extended permissions, the dialogue returns back an access_token, but it's not valid for the extended permissions. Code example below.
window.fbAsyncInit = function () {
FB.Canvas.setAutoGrow();
FB.init({
appId: facebookAppId,
status: true, // check login status
});
function updateFBInfo(response) {
console.log('updateResp:');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function (info) {
displayUserInfo(info, response);
});
}
else {
FB.login(function (loginResponse) {
if (loginResponse.authResponse) {
FB.api('/me', function (info) {
displayUserInfo(info, loginResponse);
});
}
}, { scope: 'email,manage_pages,offline_access,publish_stream' });
}
}
FB.getLoginStatus(updateFBInfo);
};
I guess my question is either, what am I missing (this has got to be something easy), or is there a way to check and see if the returned acces_token is actually valid?
You need to check explicitly for the permission before proceeding. If they haven't provided the necessarily permissions, you need to display FB.login() with the necessary scope again.
Here is the code for checking permissions:
FB.api('/me/permissions', function (response) {
var perms = response.data[0];
// Check for publish_stream permission in this case....
if (perms.publish_stream) {
// User has permission
} else {
// User DOESN'T have permission. Perhaps ask for them again with FB.login?
}
} );

Facebook Application - Unable to get inside FB.getLoginStatus and execute something

I'm using the following code:
alert('Inside login status');
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
//user is logged in and connected
userid = response.authResponse.userID;
accesstoken = response.authResponse.accessToken;
alert('Welcome' + response.authResponse.name);
//return 'connected'
//do something with user
}
else if (response.status === 'not_authorized') {
//user is logged into facebook but not connected to the app
//do something
alert('Like us to get started');
//return 'not_authorized'
}
else {
//the user even connected to facebook
//button.innerHTML = 'unknown'
//button.onclick = function () { login(); }
alert('login to get started');
//login();
//return 'unknown'
}
});
I have defined the above code in a function that is called in the async init. (The async init is defined in the code behind, I'm registering the script to the head of the page on page load) The first alert is called which means I get into the function, but the rest of it doesn't seem to work. No alerts are displayed after that.
On debugging using breakpoints, I was just directed to the end of the function. No clue why this happens. Am I missing something?
Make sure that you have specified correct callback URL in facebook application setting. And If you are testing it on local machine, have you created virtual directory?
It's just the way it runs on my system. I deployed it to a different environment and it worked like a charm.