How do I create a custom login url for Facebook graph API? - facebook

I want to get the user name and creds from a Facebook user so he can authenticate as a user against my application. Right now, getLoginUrl returns a site at Facebook, which works. How do I avoid the user jumping from my site to Facebook? Can I show a custom form to get the username and password and send that to FB directly?

You have to use the FB.login function from the Javascript API for this,
function fbAuth() {
FB.login(function(response) {
if (response.authResponse) {
alert('User fully authorize the app.');
} else {
alert('User canceled login or did not fully authorize the app.');
}
}, { scope: 'email,friend_likes' });
}
then you can call it with an onclick event or any other method you want:
Login
Documentation: https://developers.facebook.com/docs/reference/javascript/FB.login/

Related

Laravel. OAuth facebook authentication. Check if user logged out from facebook

I want to know if there is way to check if user logged out from facebook? Or I need every time to do like:
$fb = OAuth::consumer( 'Facebook' );
Yes, you can. Suscribe to the authResponseChange event. Then you can know when the user login/logout.
Here is a javascript example:
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status === 'connected') {
//User login
}
else
//User logout
});
}
This example is in Javascript but i'm sure you can do the same in PHP. Just check the Facebook API documentation.

Initiate login for custom stories

I have created a custom story to be posted on facebook timeline when user clicks on a share button.
While i was logged into facebook, I could successfully post on timeline using the following code
function postLike() {
FB.api(
'https://graph.facebook.com/me/og_pricepan:compared',
'post',
{ product: objectToLike,
privacy: { 'value': 'SELF'}
},
function (response) {
if (!response) {
alert('Error occurred.');
} else if (response.error) {
document.getElementById('result').innerHTML =
'Error: ' + response.error.message;
} else {
document.getElementById('result').innerHTML =
'<a href=\"https://www.facebook.com/me/activity/' +
response.id + '\">' +
'Story created. ID is ' +
response.id + '</a>';
}
}
);
Now, when the user is not logged into facebook, I get the following error:
Error: An active access token must be used to query information about the current user.
Shouldn't the login window get open by itself asking the user to login into facebook or I need to do something on my end to handle this situation?
Also, do i need to submit my application for review before users can create custom stories from my website to their timeline?
Shouldn't the login window get open by itself asking the user to login into facebook or I need to do something on my end to handle this situation?
No. You need to explicitly login the user by following the Facebook Login Flow to request an access token
Also, do i need to submit my application for review before users can create custom stories from my website to their timeline?
Yes and No. No, because it's not like you will have someone from Facebook Support Team going through your app to make sure your app ticks all the boxes. Yes, because you will need to register your app with Facebook. So, technically all you need to do is register with Facebook as a developer and register your app in order to get an app secret and API key. You can have your app in "Sandbox mode", create test users and test your integration with Facebook before you publish it live. There's few requirements you need to meet before publishing it, such as providing set of logos of a specific size, etc. Here's a blog explaining part of the process, but really, the process is very straight-forward and you should be able to get through easily by yourself.
Now, the error you are getting says it all. There's no user logged in to facebook. The endpoint you are trying to post to, requires a active valid access token, if it doesn't exist then there's no context, no user authenticated and you need to explicitly authenticate the user by invoking the login dialog...
FB.login(function(response) {
if (response.authResponse) {
// The person logged into your app
} else {
// The person cancelled the login dialog
}
});
If you need to know whether a user is logged in or not, you can follow to simple approaches:
1.By invoking the FB.getLoginStatus function
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
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.
}
});
2.Or, by subscribing to the auth.authResponseChange event when initializing the framework and setting the status option to true
There's a full-legged tutorial on Facebook Developers Site for Javascript SDK explaining both approaches.
Hope it makes sense

how to determine if a user has confirmed or canceled signin via facebook javascript sdk

I am logging users into a web site via facebook's Javascript SDK. After the user clicks the "log in through fb" button, I would like to generate different responses depending on whether the user click "ok" or "cancel" in facebook's confirmation. Here is the code I use when they click the fb button.
$("#fb").click(function() {
FB.login(function(response){
FB.api('/me',function(response){
// I believe the test to see if they are logged in or not should go here
});//FB.api
},{scope:'publish_actions, email, user_likes, user_location'});
});
Any advice is appreciated. Thanks!
According to facebook's documentation:
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
alert('a');
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
alert('b');
} else {
// the user isn't logged in to Facebook.
alert('c');
}
});
It worked for me. Hope it helps someone else.

Facebook Authorization redirecting to "The page you requested was not found" on mobile devices

I've been building out a social gifting platform, and have had an application running for a few months now at: http://egift.me/promos/venue/facebookconnect
There is a simple user flow for the app. Click the Login/Connect facebook button, login &/or authorize the application, and you're redirected to a 'thank you/confirmation' style page.
If I step through this on a standard browser, I have no problems. If I step through this on mobile, I ultimately get the lovely "The page you requested was not found" page.
I don't have a Mobile Web url configured, I'm simply using Website with Facebook Login. Even when I have tried adding a mobile web url (it's the same base URL, I'm using View Switching to serve up a desktop vs mobile optimized view), I get the same issue.
Anyone have any idea what's going on? Is there any additional information I can provide?
[UPDATE]
This works (be sure to change your scope):
//instead of onClick could just as easily use something like jQuery event binding
<button onClick="loginUser();">Login</button>
<script type="text/javascript">
function loginUser() {
FB.login(function (response) { }, { scope: 'YOUR_SCOPE_HERE' });
}
FB.getLoginStatus(function (response) {
FB.Event.subscribe('auth.authResponseChange', handleResponse);
});
handleResponse = function (response) {
document.body.className = response.authResponse ? 'connected' : 'not_connected';
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
} else if (response.status === 'not_authorized') {
// the user is logged in to FB, but has not yet authenticated your app
} else {
// the user is not logged in to FB
}
};
</script>
For mobile web apps, I'd recommend to use FB.login via the SDK itself rather than through the use of the login button. If nothing else it gives you greater programmatic control of the user flow & experience.
The canonical "simplest ever social app" snippet is:
FB.login(function(response) {
if (response.authResponse) {
console.log('Fetching user info');
FB.api('/me', function(response) {
console.log('Hello ' + response.name + '!');
});
} else {
console.log('User cancelled login or did not fully authorize');
}
});
Also you're including the JS SDK for the plugin anyway, so there's no payload overhead. More details on FB.login here: https://developers.facebook.com/docs/reference/javascript/FB.login/

Authorization trouble with FB.login method

When I ask for user authentication of my app, I use this dialog:
FB.login(function(response)
{
if (response.authResponse)
{ ...}
else
{...}
},
{scope: 'publish_stream' });
My question is if the dialog is supposed to ask the user to login first and then ask for app authorization? Can i make the authorizarion dialog not to ask for login first? Since the user is already logged in to facebook?
If the user has already logged in, FB will handle it and directly show the auth dialog instead of the login dialog