Facebook FB.ui oauth dialog error - facebook

I'm having problems making a dialog box to ask for a user to log into my facebook tab to enter a contest.
We're trying to use the FB.ui oauth dialog box with the following code.
FB.ui({
client_id: '12345',
method: 'oauth',
scope: 'email,user_birthday',
response_type: 'token',
redirect_uri: "https://mydomain.com/test.php" // Change this to proper address
});
When we include the redirect_uri we get the following message
API Error Code: 100 API
Error Description: Invalid parameter
Error Message: The "redirect_uri" parameter cannot be used in conjunction
with the "next" parameter, which is deprecated.
We are not using the next parameter anywhere though, so not sure why it is saying that.
When we take away the redirect_uri we get the following message.
API Error Code: 191 API
Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.

I would use the FB.login method as opposed to this given you are operating within a tab application.
Within your FB.init:
window.fbAsyncInit = function(){
FB.init({
appId: 'your_app_id',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.Canvas.setAutoGrow(true);
};
FB.getLoginStatus(function(response){
if(response.status == 'connected'){
callyourfunctiontoentercompetition();
}else{
FB.login(function(response){
if(response.status == 'connected'){
callyourfunctiontoentercompetition();
}else{
handlethecancelpermissions();
}
});
}
});

Turns out there was a few things wrong, In my app settings I had to select "Website" and put in my site's URL, this allowed me to add the "app domain" and save it. This cleared up the 191 Error. Having website not checked off your App Domain will not save. After that it was rather simple.
Here the code that I used.
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelURL : 'http://www.your_domain.ca/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
// This should be fired on a user initiated action
FB.ui({ method: 'oauth', perms: 'email' }, function() { callback() });
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
function callback() {
FB.getLoginStatus(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
//Get user information from here
});
}
});
}
</script>

make sure the request that u r making is coming from the same domain as the one you have registered in facebook api

In a tab you don't need to pass any parameter other than method and perms(or scope when Facebook changes it)
FB.getLoginStatus(function(response){
if(response.status == 'connected'){
alert("User connected");
}else{
FB.ui({
method: 'oauth',
perms: 'email,user_birthday' //change perms to scope when Facebook supports it
}, function(response){
if(response.session){
alert("User connected");
}else if(response.installed != '1'){
alert("User canceled");
}
});
}
}
)

Related

Permission to access emails and facebook

I create a app that use login on Facebook, but I can not capture the user's email address, I know I need to use scope, but do not know how. How can I do this?
This is my code:
window.fbAsyncInit = function() {
FB.init({
appId : '<?= $this->getAppId(); ?>', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
FB.login()
} else {
FB.login()
}
});
}
// 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));
// Here we run a very simple test of the Graph API after login is successful.
// This testAPI() function is only called in those cases.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
console.log( response.email);
});
}
You pass in any additional permissions you want as an argument (comma-separated for multiple permissions) to FB.login(). IN your case, you want email:
FB.login(function(response) {
// handle the response
}, {scope: 'email'});
The FB JS SDK Documentation very clearly explains this. Please take the time to read it.
I resolved the problem, when use FB.Event.subscribe, we need put the permissions in html and not in FB.login() function, using this way:
fb:login-button show-faces="true" perms="email,user_birthday" width="200" max-rows="1
Thanks

To get Facebook Friends email address [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get Facebook friends' email?
In my application i have to retrieve user's friends email address.I tried the following code,But i didn't get the friends email address.
My code:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxx',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth :true
});
function login(){
/* Show User's Name*/
FB.api('/me/friends', function(response) {
alert("user name:"+response.name);
alert("user mailid:"+response.email);
// document.getElementById('login').style.display = "block";
// document.getElementById('login').innerHTML = response.name + " succsessfully logged in!";
});
//window.location="http://google.com"; // Redirect to Another Page.
}
But in the alert is not showing the mail id.I googled for this ,i found that email is not public property ,so what i can i do for retrieving the mail id.
As far as I know friend's email address is not accessible.
Thanks
Kaushik
First Initialize FB Javascript SDK as following.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'YOUR_APP_ID', // App ID from the App Dashboard
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
});
// Additional initialization code such as adding Event Listeners goes here
};
// Load the SDK's source 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>
The above code will initialize your FB SDK. Now in JavaScript write following function.
Refer this document for more details.
function initiateFB Login()
{
FB.login(function(response) {
if (response.authResponse)
{
//Login Success if you want you can initiate request for friend list here only.
FB.api('/me/friends/fields=id,username', function(resp)
{
/// Process the resp over here. This resp will be JSON response of friend request
}
);
}
else
{
console.log('User cancelled login or did not fully authorize.');
}
});
}
You can get the username i.e. FB User Name. Below is the request for that.
me/friends?fields=id,username
This request will give you a JSON response of following format.
{
"data": [
{
"id": "FB_ID",
"username": "FB_USER_NAME"
},
{
"id": "FB_ID",
"username": "FB_USER_NAME"
}
]
}
You can parse this and get the username.
Hope this helps.
Thanks
Kaushik

Getting facebook's JSON?

I need a little help with getting some JSON out of facebook, basically all I need to do is get the status text and post it to a website.
I'm using the method below, but the access_tokens with this method expires every hour.
$.getJSON('https://graph.facebook.com/SOME_ID?fields=statuses.limit(10).fields(message)&access_token=SOME_ACCESS_TOKEN', function(data) {});
Note that i got the URL above from facebook graph API explorer.
any other way to achieve what I am trying to do?
update:
I used FB.api, but i'm not sure if I've done it correctly as it is sending me this error
"An access token is required to request this resource."
window.fbAsyncInit = function() {
FB.init({
appId : 'My_app_id', // App ID
channelUrl : '//myHost.com/facebook/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.api('USER_ID?fields=statuses.fields(message)', function(response) {
console.log(response);
});
// Additional initialization code here
};
// 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));
Note that i replaced (my_app_id, myHost.com and USER_ID) with the right values.
thanks in advance.
Assuming you have the Javascript SDK implemented i would suggest using the FB.api method.
https://developers.facebook.com/docs/reference/javascript/FB.api/
The user needs to be logged in to Facebook and allow your app before you can call the API, update your code to this:
window.fbAsyncInit = function() {
FB.init({
appId : 'My_app_id', // App ID
channelUrl : '//myHost.com/facebook/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('USER_ID?fields=statuses.fields(message)', function(response) {
console.log(response);
});
}
});
};
More information on the getLoginStatus status: http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
You will also need to also need to ask the permission user_status see the doc for more details: https://developers.facebook.com/docs/authentication/permissions/
You should ask for permissions when calling FB.login:
FB.login(function(response) {
// handle the response
}, {scope: 'user_status'});
https://developers.facebook.com/docs/reference/javascript/FB.login/

FB.Event.subscribe('auth.login') won't trigger everytime ?

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'MY_APP_ID',
status : true,
cookie : true,
xfbml : true
});
FB.Event.subscribe('auth.login', function(response) {
$.ajax({
url: "http://example.com/fbconnect",
success: function(){
window.location.reload();
}
});
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
<div class="fb-login-button" data-perms="email,user_birthday,user_hometown,user_location">Login with Facebook</div>
It's working with only two cases.
1- when a user is NOT logged in and log in to facebook it triggers.
2- when a user is logged in but didn't authorize the app yet it triggers.
the third scenario which is not working is when a user is logged in and He is already authorized the app the FB.Event.subscribe('auth.login) won't trigger !!!
I encountered the same thing as you and I found a function, "getLoginStatus", that might be of interest for you.
When the user is already logged in and has granted your app all the permissions, then there is no need to log in again and therefore the event won't be triggered.
However, if the response of the "getLoginStatus" is "connected" you can do the stuff you want to do when the user is already logged in and has granted your app all the permissions.
The complete script might look something like this:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'APPID', // App ID
channelUrl : 'CHANNEL_URL', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to 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;
FB.api('/me', function(response) {
if (response.name != undefined ) {
console.log("Welcome, " + response.name);
}
});
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but not connected to the app
} else {
// the user isn't even logged in to Facebook.
}
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
Got the exact same issue here and it's driving me nuts. Not liking the Facebook Javascript SDK atm.
It's really complex with the status check in FB.init() and makes it even harder when you put facebook comments on the page...
A workaround I just tested is to have your own login button and use the FB.login() method, there you can hook in to the callback (this replaces your code for the auth.login event).
window.fbAsyncInit = function () {
FB.init({
...
});
$('#fb-login-button-custom').show().click(function () {
var scopeList = $(this).data('scope');
FB.login(function (response) {
alert('FB.login callback');
console.log(response);
document.location.href = document.location.pathname + '?fblogin=1';
}, { scope: scopeList });
});
});
That default login event is actually only triggered when the user authorizes your application. So when loging out the user, you could revoke the authorization. But then every time they log in they have to 'allow' the app.
Using FB.Event.subscribe('auth.statusChange', handleStatusChange); will notify you status changes..
hope it helps

facebook app reloading in loop, login logout events getting fired automatically inside window.fbAsyncInit

I am developing a facebook (iframe) app.
I have included following code at bottom of my page:
<script>
window.fbAsyncInit = function() {
FB.Canvas.setAutoResize();
FB.init({
appId : '<?php echo FACEBOOK_APP_ID; ?>',
session : '<?php echo json_encode($session); ?>',
status : true,
cookie : true,
xfbml : true
});
// whenever the user logs in / out, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
The problem here is, it seems that auth.login and auth.logout events are getting fired immediately inside window.fbAsyncInit function, even if I do not log-out from (or log-in to if logged out) my facebook profile.
I am doing no such action, even then these events seem to get fired, which causes their handler functions to reload my app (window.location.reload();), and the app goes into infinite reload loop.
Please guide me.
Thanks
From my experience instead of using auth.logout and auth.login events you may try the following code:
FB.Event.subscribe('auth.authResponseChange', function(response) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to 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 not connected to the app
} else {
// the user isn't even logged in to Facebook.
}
});
});
I've been stuck trying to solve this bug for a few days. The thing is that auth.login event is fired when the SDK is loaded even if you're already connected thus creating an infinite loop. I've solved it when i made the code listen to the authlogin event and reloading the page only if the user is not connected.
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo AppInfo::appID(); ?>', // App ID
channelUrl : '//<?php echo $_SERVER["HTTP_HOST"]; ?>/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Listen to the auth.login which will be called when the user logs in
// using the Login button
FB.getLoginStatus(function(response) {
console.log (response.status);
//this is useful if you want to avoid the infinite loop bug
//it only reloads page when you log in
if (response.status != 'connected') {
FB.Event.subscribe('auth.login', function(response) {
// We want to reload the page now so PHP can read the cookie that the
// Javascript SDK sat. But we don't want to use
// window.location.reload() because if this is in a canvas there was a
// post made to this page and a reload will trigger a message to the
// user asking if they want to send data again.
//window.location = window.location;
//Im using the window.location.reload()
window.location.reload();
});
}
});
FB.Canvas.setAutoGrow();
};
// Load the SDK Asynchronously
(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";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Hope this works!
Anyway this is not the only problem i have. please check this post and help me if you can
impossible to read $_SESSION vars on PHP facebook app