FB.api is not a function - javascript

I'm trying to pull all of the posts on a users wall. I got an example running that connects to FB okay, but when I try to use FB.api to get the username (I am definitely successfully connected), I get FB.api is not a function. Some searching found this which suggests that I'm using the wrong API link, but when I try the new one (which seems to connect using the same method) everything firebug NET panel says it loads successfully, there are no errors. . . nothing happens at all.
Here is the code (replace the http://connect.facebook.net/en_US/all.js link with http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php for the code to work up until the error I mentioned):
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script type="text/javascript">
function grab(){
FB.api('/me', function(response) {
alert(response.name);
});
}
function update_user_box() {
var user_box = document.getElementById("user");
user_box.innerHTML =
"<div>"
+ "<fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic>"
+ " Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>"
+ "</div>"
+ "try me";
FB.XFBML.Host.parseDomTree();
}
</script>
</head>
<body>
<div id='user'><fb:login-button onlogin="update_user_box();"></fb:login-button></div>
<br><br>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init("b07e858317c9069d450023b7500b4511", "xd_receiver.htm", {"ifUserConnected": update_user_box});
</script>
</body>
</html>
thanks for any and all help!

You're using methods from two different SDKs. FB.api is part of the new SDK (all.js), and FB.XFBML.Host.parseDomTree() is part of the old SDK (feature loader). For example, to parse the dom tree with the new SDK, you call FB.XFBML.parse(). I recommend you pick one of the two SDKs, preferably the new SDK if you are just starting since the old SDK is deprecated.

I guess you are not intializing correctly. I had similar issues... it was not clear to me which one is oAuth2.0 and which FB-Connect. It seemed confusing. I followed the example (below), it worked for me. See here
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
Hope this helps.
On a side note, you might need extended permission to access publish_stream, read_stream. You should refer here

Typeoneerror is right, you're mixing SDK's.
Here's how I'm initializing the newer JS SDK, using event.subscribe to be sure the SDK is initialized AND the user has authenticated on the JS side before moving forward with any API calls or letting my game try to load etc.
<div id="fb-root"> </div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'YOUR_APP_ID', status: true, cookie: true, xfbml: true});
FB.Canvas.setSize({ width: 760, height: 1000 }); // use this to increase canvas height
FB.Event.subscribe('auth.login', function(response) {
// You can call your function that requires FB.whatever here...
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
I'm also using the PHP SDK to perform the initial auth... If the user is not authed, I echo a JS redirect to our the login url (available from the PHP SDK method getLoginUrl). Hope that helps.

Related

Search twitter API code-js

I need to use Twitter's Search API. I read twitter's developer documentation and it says that client side authentication is not recommended anymore. But, since mine is really a small application and on a very tight deadline, I decided to use codebird-js.
Here is my code:
<script type="text/javascript" src="sha1.js"></script>
<script type="text/javascript" src="codebird.js"></script>
<script type="text/javascript">
function loginTwitter() {
var cb = new Codebird();
cb.setConsumerKey("my_consumer_key","my_consumer_secret");
cb.setToken("my_token", "my_token_secret");
cb.__call(
"search_tweets",
"q=Twitter",
function (reply) {
alert("hey");
},
true
);
}
</script>
Dont think there is any problem with the callback of search tweets, since this is what is documented in codebird-js.Kindly suggest any alternatives to make the code work.
Also, I enabled the option "Allow application to sign in with twitter" in application settings.
You have to make some syntax corrections to your code and to call the "loginTwitter()" function; the code should be:
<script type="text/javascript" src="sha1.js"></script>
<script type="text/javascript" src="codebird.js"></script>
<script type="text/javascript">
function loginTwitter() {
var cb = new Codebird();
cb.setConsumerKey("my_consumer_key", "my_consumer_secret");
cb.setToken("my_token", "my_token_secret");
cb.__call(
"search_tweets",
"q=Twitter",
function (reply) {
alert(JSON.stringify(reply)); //do something with the result
},
true
);
};
loginTwitter();
</script>
And don't forget to download the codebird for javascript and put its files into the same folder as your main file (according to the path that you have put in your script).

Unsafe JavaScript attempt to access frame...Protocols must match

Here's my situation: I have a Javascript plugin that when clicked launches a popup on the third-party sites that host it. That popop then displays an IFRAME, in which I am using Facebook as a login method.
When the popup is launched, it recently started giving an error:
Unsafe JavaScript attempt to access frame with URL
http://{THIRD-PARTY-SITE-GOES-HERE} from frame with URL
https://s-static.ak.facebook.com/connect/xd_arbiter.php?version=18#channel=…%3Dtabmodule%26utm_term%3D200000%26fb_xd_fragment%23xd_sig%3Df2ade8e518%26.
The frame requesting access has a protocol of 'https', the frame being
accessed has a protocol of 'http'. Protocols must match.
The IFRAME itself is on https and used to work properly. I'm not sure why Chrome is trying to access the parent page. I have no control over the parent pages, so I can't make them https.
Here is my FB init code from within the IFRAME:
window.fbAsyncInit = function()
{
FB.init({
appId: '{myappid}',
status: true,
cookie: true,
xfbml: true,
oauth: true,
channelURL : 'https://degree3.com/channel.php'
});
FB.getLoginStatus( function(response)
{
if (resp = response.authResponse)
{
$( '#fb_button' ).attr( "onclick", "signinViaFacebook( response.authResponse.userID, response.authResponse.accessToken );" ).show();
}
});
};
(function()
{
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
Any ideas how I can fix the error?
You can't use facebook login inside an iframe it will never work. Dont even bother trying I have spent several frustrating hours trying to do the same. Even if you solve the https problem you have right now, you will then face an X-Frame Options error because of an option set in the response header by facebook which accepts only requests from same origin(that is facebook).
What I'am basically trying to say is that facebook cannot be launched in an Iframe for security reasons. Your best bet in your current predicament is to open another popup with facebook login in it. Iframe simply wouldn't work. I know it wouldn't look elegant but it is the only way to go or you redirect your popup itself to facebook and have a callback mechanism to redirect back to your plugin.
Hope it helps.

Facebook javascript events not fired any way i turn it

I am using Facebook JavaScript SDK with my site, there is an option there to set events when user login/logout from Facebook so i can show him notifications in my website.
can't manage it to work, tried everything, also using offline_access permission.
here is my html code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="blabla" />
<meta name="keywords" content="blabla" />
<meta property="og:title" content="blabla" />
<meta property="og:url" content="http://XXX/" />
<meta property="fb:app_id" content="XXX" />
<title>Facebook Notifier</title>
<link href="<?php echo base_url(); ?>styles/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.cookie.js"></script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/global.js"></script>
</head>
<body>
<div id="fb-root"></div>
<div id="my_div">
</div>
</a>
</body></html>
here is my js code:
FB.init({
appId : 'XXX',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true, // enables OAuth 2.0
channelUrl : 'http://XXX/channelUrl'
});
FB.getLoginStatus(function(response) {
if (response.authResponse) {
connected = true;
}
else{
connected = false;
}
}, true);
FB.Event.subscribe('auth.authResponseChange', function(response) {
if(response.authResponse) connected = true;
else connected = false;
});
in the end of this js code i have a function that run every few seconds checking the connection variable, its a global one. and it not changed i tried to login/logout from facebook and no event fires.
only when i refresh the page i can get the user status.
in firefox firebug console i get the message (also tried in chrome, ie):
uncaught exception: Error: https://www.facebook.com Proxy.InstallTrigger
i tried to solve it by doing everything i can find about this error in google and also in stackoverflow and still not working.
btw: when i wrote XXX in the original files its my url or my appid.
thanks.
I've always been a bit skeptical about the auth. events the Facebook fires so in your case I'd prob just setup a poller myself to check the auth status of the connected users. Like this:
var connected;
FB.init({
appId : 'XXX',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true, // enables OAuth 2.0
channelUrl : 'http://XXX/channelUrl'
});
FB.getLoginStatus(handleUserStateChange);
FB.Event.subscribe('auth.authResponseChange', handleUserStateChange);
function handleUserStateChange(response) {
connected = !!response.authResponse;
}
setInterval(function() {
FB.getLoginStatus(handleUserStateChange, true);
}, 10000) // poll every 10 seconds
Also as you defined status: true in the FB.init config object you don't need to pass the second parameter to FB.getLoginStatus at the start as that forces a check to Facebook instead of reading from the stored variable (which is already the latest information as we just initd).
It looks like you might have a variable scope problem with connected.
Something like this might help:
var connected; // define/initialize connected variable outside functions
FB.init({
appId : 'XXX',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true, // enables OAuth 2.0
channelUrl : 'http://XXX/channelUrl'
});
FB.getLoginStatus(function(response) {
if (response.authResponse) {
connected = true;
}
else{
connected = false;
}
}, true);
FB.Event.subscribe('auth.authResponseChange', function(response) {
if(response.authResponse) connected = true;
else connected = false;
});
I would use console.log() from within your functions that respond to subscribed events to see what is really happening (or not happening). Then write your real code based on what console.log() tells you.
I am doing FB.Event.subscribe in a function that is invoked on document ready event:
<script>(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/all.js#xfbml=1&appId=...";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
$("document").ready(function(){
FB.Event.subscribe('edge.create',
function(response) {
alert('You liked the URL: ' + response);
});
});
</script>
Or here there is another example of making sure that FB is initialised before you do any invocations.
I'm all new into FB development and ran into the same issue a couple days ago.
I tried pretty much all answers and solutions that are out there but nothing
worked out. The damn event wouldn't want to fire no matter what I did in my
latest VS project. However I remembered getting this to work in my first VS project
where I followed a tutorial on the same matter. After a while I checked that initial project and to my surprise it fired there and not in my other one.
Finally I realized that the Canvas Url on FB (app settings) was the culprit :
h t t p: // localhost:15497/
It was set to my localhost with the port used in my other project. I changed the port number and suddenly it also worked in my other project.
Make sure that the canvas url in the FB app settings matches where your Visual Studio runs the app. Also change the value once you deploy your web app.
If you're in 2022 wondering why it won't fire. Enable 3rd party cookies, this worked for me.

How do I set locale for FBML on old Facebook Connect api?

A website I am working on has to use the Facebook Connect "Old JavaScript Client Library" API (the new "JavaScript SDK" has not yet implemented all the features we need).
Additionally, we need to have support for multiple languages. I know that when using the new API, you may specify a locale when you initialize FB Connect (like so):
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: 'XXXXXXXX',
status: true,
cookie: true,
xfbml: true
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/fr_CA/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
BUT, since I have to use the old API, how can I set the locale when I am initializing like so:
<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"></script>
<script type="text/javascript">
FB_RequireFeatures(["XFBML"], function(){
FB.Facebook.init("XXXXXXXXX", "/xd_receiver.html");
})
</script>
I'm not sure that it is relevant to the issue at hand, but should it matter, the bit of FBML I am rendering is the fb:comments tag.
http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/fr_CA

API Key Error in Facebook Connect (javascript sdk)

I am trying to implement Facebook Connect on a website. I am trying to work with Javascript SDK of Facebok. I am new to it and unfortunately most of links provided in Facebook WIKI are out of date... Returning 404 not found. Anyway I added this code before the ending </body>:
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
<div id="fb-root"></div>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"> </script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '12344', // my real app id is here
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : false // parse XFBML
});
FB.login(function(response) {
if (response.session) {
if (response.perms) {
alert('user is logged in and granted some permissions');
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
} else {
alert('user is logged in, but did not grant any permissions');
// user is logged in, but did not grant any permissions
}
} else {
alert('user is not logged in');
// user is not logged in
}
}, {perms:'email'});
</script>
And I have a login button at some other place (much before the above scripts) in the same page rendered with:
<fb:login-button v="2">Connect with Facebook</fb:login-button>
This button renders as a normal fb connect button and clicking it opens a new popup window as it normally should. Problem is that it shows 'invalid api key specified' error. On inspecting address bar of popup, I see that api_key=undefined is passed to it :(
Any idea about this? I am trying to fix this for last 5 hours now... Please help me found out why correct API key is not being passed to popup window.
Thanks in advance.
I'm doing the same thing, and i found an example that is very simple to implement and understand (here thay use jQuery, but you can do the same without libraries):
<body>
<div>
<button id="login">Login</button>
<button id="disconnect">Disconnect</button>
</div>
<div id="user-info" style="display: none;"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
// initialize the library with the API key
FB.init({ appId : '12344' });
// fetch the status on load
FB.getLoginStatus(handleSessionResponse);
$('#login').bind('click', function() {
FB.login(handleSessionResponse);
});
$('#disconnect').bind('click', function() {
FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
clearDisplay();
});
});
// no user, clear display
function clearDisplay() {
$('#user-info').hide('fast');
}
// handle a session response from any of the auth related calls
function handleSessionResponse(response) {
// if we dont have a session, just hide the user info
if (!response.session) {
clearDisplay();
return;
}
// if we have a session, query for the user's profile picture and name
FB.api(
{
method: 'fql.query',
query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
},
function(response) {
var user = response[0];
$('#user-info').html('<img src="' + user.pic + '">' + user.name).show('fast');
}
);
}
</script>
</body>
Look here for the entire code, and here you can find other resource.
I had set my facebook connect address to have www and I was accessing my test site with non www, when I switched to the www version fb connect worked fine.
You need to set the canvas post-authorize url, and make sure that the base canvas url is a prefix of the post-authorize url.

Categories

Resources