How can I detect that a user signed into Facebook? - javascript

Here is my scenario. When a user opens a browser and goes to facebook.com to sign himself in, I want to be able to detect that and init my Facebook application sign-in process.
Is that possible? I'm asking because I noticed that signing into Facebook itself doesn't get my app signed in automatically, which is not good.
I'd prefer JavaScript client code.

UPDATE: Here you have a FULLY WORKING example. The only thing you will need to do is to replace the APP_ID for whatever ID you get when sign your app at: http://www.facebook.com/developers/ and the port you may use for the localhost test. This detects when the user logs in even in another browser window. It is obviously extremely basic but proves the point that is feasible with the tools from the graph API.
You can add this code to detect when the user logs in. It is the same oauth log in whether the user clicks on a <fb:login-button> or logs directly onto facebook.com:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Hello App Engine</title>
<SCRIPT type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></SCRIPT>
</head>
<body>
<h1>Hello!</h1>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
appId : APP_ID,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
/*
* Authentication functions
*/
FB.Event.subscribe('auth.login', function (response) {
welcomeMsg(response);
});
FB.Event.subscribe('auth.logout', function (response) {
alert ("Good bye!");
});
if (FB.getSession() != null) {
FB.api('/me', function(response){
console.log(response.name);
welcomeMsg(response);
})
} else {
window.setTimeout(function(){window.location.href="http://localhost:8888"},5000);
}
function welcomeMsg(userData) {
console.log ("Welcome " + userData.name);
}
</script>
</body>
</html>

You can subscribe to the auth.login event.
http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

There are many Facebook images that you can see only if you're logged into Facebook. You can make the user attempt to load one of these images, and then detect if it loaded or not. It's a total trick.

Related

Google One-Tap-Login - "No credentials avaliable" error

I am trying to follow the simplest of examples of how to setup up Google's "
One tap sign-up and automatic sign-in" in a web page. Just copy and pasting the code provided by google, I get an exception "No credentials available". Why? How to fix it?
This is my web page content ...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<script src="https://smartlock.google.com/client"></script>
<script>
console.log( "window.location.origin=" + window.location.origin);
window.onGoogleYoloLoad = (googleyolo) => {
console.log("Google one tap ready");
};
const retrievePromise = googleyolo.retrieve({
supportedAuthMethods: [
"https://accounts.google.com",
"googleyolo://id-and-password"
],
supportedIdTokenProviders: [
{
uri: "https://accounts.google.com",
clientId: "xxxx-86peetl434op89ug41lg1vv8sjspfupp.apps.googleusercontent.com"
}
]
});
</script>
</head>
<body>
Content of the document......
</body>
</html>
The Authorized JavaScript origins has been correctly setup. I am confident of that. The URL for the above content is: https://s3-ap-southeast-2.amazonaws.com/sbd-aws-sdk-delphi-22/index.html
Upon hitting the page (browser=Chrome), the resultant javascript console is ..
window.location.origin=https://s3-ap-southeast-2.amazonaws.com
Google one tap ready
index.html:1 Uncaught (in promise)
a {type: "noCredentialsAvailable", name: "OpenYoloError", message:
"noCredentialsAvailable: No credential is available for the current user."}
message:
"noCredentialsAvailable: No credential is available for the current user."
Context: When I make this web page hit, I am logged into Google.
What am I doing wrong?
The problem is that there is currently no user signed in. The library has very little support for this, as a work around, when no Google Accounts are active and no credentials are available. You can simply show the traditional Google Sign-In button and have the user click to proceed through the flow to activate their Google Account in the browser.
https://developers.google.com/identity/sign-in/web/

How to get posts from facebook page using javascript or php

I want post from my facebook page, can anyone please tell me the steps to do it? I see the solutions in web but can't getting what to do, can anyone help me to do this? I just have this code.
<html>
<script src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<body>
<p>Log In with the JavaScript SDK</p>
<script>
logInWithFacebook = function() {
FB.login(function(response) {
if (response.authResponse) {
alert('You are logged in & cookie set!');
// Now you can redirect the user or do an AJAX request to
// a PHP script that grabs the signed request from the cookie.
} else {
alert('User cancelled login or did not fully authorize.');
}
});
return false;
};
window.fbAsyncInit = function() {
FB.init({
appId: 'my-app-key',
cookie: true, // This is important, it's not enabled by default
version: 'v2.2'
});
};
</script>
</body>
</html>
In your application you have inserted wrong domain details. Check your settings.
Select the correct App and click in the edit button;
Check the URLs & paths are correctly entered and are pointing to the site where you have installed Ultimate Facebook plugin.

Facebook example JS login not loading

I'm trying to replace my PHP login for my canvas app with a JS one and have copied the Facebook example almost exactly from here https://developers.facebook.com/docs/reference/javascript/ and here https://developers.facebook.com/docs/reference/javascript/FB.login/. However even though the alert prompt is opening the prompt for the user to accept the permissions isn't. Can anyone see why from the code below? I've been at this for hours now and I can't see how my code really differs from what was suggested. Could it have something to do with the browser protection not running javascript?
<?php
require_once 'config.php'; // This contains app information
//include_once 'scripts/mysql_connect.php'; // This is the database connection configuration
require_once 'Database.php';
$fbconfig['appUrl'] = "https://localhost/FindAFlatmate-Dev/";
?>
<html>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Profile Page</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '127865210690483', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
alert('Api loaded');
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
</body>
</html>
However even though the alert prompt is opening the prompt for the user to accept the permissions isn't. Can anyone see why from the code below?
You are calling FB.login directly on page load, with no user interaction – so it’s most likely your browser’s popup blocker that is preventing the login dialog from showing at all.
The general recommendation for FB.login is to call it only on explicit user interaction, like the user clicking on a link/button that says “login” or something. This way the popup FB.login opens is less likely to be blocked, since the default configuration for popup blockers in current browsers is to block only popups that are opened “automatically” without the user requesting them in any way.

Facebook App to post to Facebook Page

Can someone please provide an up-to-date guide on how to setup a Facebook App linked to a Business Page to allow posting from an external website?
I have created a Website with Facebook Login App and I am using the JavaScript SDK to posting of content from our website to our Business Page.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>My Feed Dialog Page</title>
</head>
<body>
<div id='fb-root'></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<p><a onclick='postToFeed(); return false;'>Post to Feed</a></p>
<p id='msg'></p>
<script>
FB.init({appId: "OURAPPID", status: true, cookie: true, frictionlessRequests: true});
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
link: 'http://www.ourdoamin.com/test.jsp',
picture: 'http://www.ourdoamin.com/image.jpg',
name: 'Test Article',
caption: 'Here We GO',
description: 'This is a test.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
</script>
When I click the Post to Feed link, I am presented with a dialogue box, the App is correctly identified in the Via field and if I enter a status and click Share, I receive a Post ID.
However, despite the app being correctly referenced and receiving a Post ID, the post actually appears on the Wall of the users account currently logged in and not the Business page.
I would prefer no login at all, just a straight post using the App to the Business Page and not a users individual account.
Can someone, please tell me where I am going wrong and how to correct this?
You can post directly to a page using the page access_token. In order to do this, you will need the manage_pages permission. Once you ask for the permission, you can get the page access_token by calling /me/accounts and noting the correct token for the page you want to post to.
Use this access_token in your API call to post to the page as an administrator / page.

FB.Canvas.setUrlHandler

Are someone succed to use the method FB.Canvas.setUrlHandler please?
I've already read the following URL's
http://developers.facebook.com/docs/reference/javascript/FB.Canvas.setUrlHandler
http://​developers.facebook.com/blog/​post/555
but it still don't work... Someone to help me please?
Thanks in advance.
Here is my source code , when I execute this, nothing gets logged in the console:
<html>
<head>
</head>
<body >
<div id='fb-root'> </div>
<script type="text/javascript" src="http://?connect.facebook.net/en_US/?all.js"> </script>
<script type="text/javascript">
FB.init({
appId : 'MY_APP_ID',
oauth : true,
status : true,
cookie : true,
oauth : true,
xfbml: true
});
function testUrlHandler(data) {
if (data.path.indexOf("test1"?) != -1)
console.log('test1');
else if (data.path.indexOf("test2"?) != -1)
console.log('test2');
else
console.log('default');
}
FB.Canvas.setUrlHandler(te?stUrlHandler);
</script>
</body>
</html>
Per the documentation, the callback is only for links on the parent iframe (Facebook UI)
Registers the callback for inline processing (i.e. without page
reload) of user actions when they click on any link to the current app
from Canvas, including:
All ticker stories
Bookmarks
Requests from the bookmarks drop-down list
Request Notifications stories.
Your links in app wont fire the event.
I've spend 3 hours trying to understand what's wrong with it. Sometimes (really rare occasions ) this thing works just fine, and sometimes FB api just ignores this callback.
Solution - set the callback with delay after init, solution: How to work with facebook setUrlHandler?

Categories

Resources