I need help to make a facebook app on my website permanent. The access token keeps expiring and i dont know how to get a permanent token.
Im using the fb.wall from "http://www.neosmart.de/social-media/facebook-wall/"
Here is my code:
<script type="text/javascript">
$(function () {
$('#example1').fbWall({
id: 'Facebookuser',
accessToken: 'accesstokengoeshere',
showGuestEntries: false,
showComments: true, max: 5,
timeConversion: 24
});
});
</script>
Instead of trying to get a permanent access token, try making a call to a function like this one each time where you have 'accesstokengoeshere':
function getAccessToken () {
FB.getAuthResponse(function(response) {
if (response.status == 'connected') {
return response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// if the user is not logged in to FB, prompt them to do so
// later might want to change this so it doesn't automatically prompt you to login
FB.login(function(response) {
return getAccessToken();
});
} else {
FB.login(function(response) {
return getAccessToken();
});
}
});
}
Related
I want my website to login with facebook but i am seeing this error.
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
// alert('login fb');
document.getElementById('login').style.display = 'none';
document.getElementById('startbtn').style.display = 'block';
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
alert('not authorized');
} else {
// The person is not logged into Facebook, so we're not sure if
alert('not login fb');
document.getElementById('login').style.display = 'block';
document.getElementById('startbtn').style.display = 'none';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : 'my app id',
oauth : true,
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.5' // use graph api version 2.5
});
</script>
It work in mycomputer and other computer.But one of Computer
Show alert (not authorized) and can not login.
How can I solve this problem.
You can invoke the login view of facebook if status is not_authorized. Try this:
if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
console.log('Please log into this app.');
fbLogin();
}
function fbLogin(){
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me/?fields=picture,name,email', function(response) {
console.log(response);
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}
The following code does not seem to work in Firefox or IE, but runs fine in Chrome. I'm hoping someone might have some insight into what I am doing wrong. It is used to run a unity game in the Facebook canvas.
<script type='text/javascript'>
var appID = 'xxxxxxxxxxxxxxxxxxx';
var connected = false;
var loggedin = false;
var purchaseResult = "Failed";
var inviteResult = "Failed";
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : appID, // App ID from the App Dashboard
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
oauth : true, // enable OAuth 2.0
frictionlessRequests : true,
xfbml : false // parse XFBML tags on this page?
});
// Additional initialization code such as adding Event Listeners goes here
//Get the current login status.
document.getElementById("text").innerText = "Getting login status";
console.log("Getting login status");
FB.getLoginStatus(function(response) {
console.log("got response");
var user_box = document.getElementById("login");
//document.getElementById("text").innerText = "Got response: " + response.status;
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;
console.log("Getting login status returned user id: " + uid);
var accessToken = response.authResponse.accessToken;
//user_box.style.visibility = 'hidden';
loggedin = true;
//document.write("Connected to facebook");
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
user_box.style.display = 'block';
FB.Event.subscribe('auth.login', function(response) {
// Let unity know the user has logged in
GetCurrentUser();
user_box.style.display = 'none';
});
} else {
// the user isn't logged in to Facebook.
FB.Event.subscribe('auth.login', function(response) {
// Let unity know the user has logged in
GetCurrentUser();
user_box.style.display = 'none';
});
user_box.style.display = 'block';
}
if (loggedin) {
GetCurrentUser();
}
}, true);
};
I have log messages in there to check what's going on, but it's only in Chrome that I get any messages after "Getting login status".
Thanks,
Andrew.
Try your Additional initialization code inside this-
if (typeof(FB) != 'undefined' && FB != null )
{
// your code
}
I am designing a Facebook app on canvas page. And I called the Facebook_login function when clicking on a button. Wnen I visit the app and click on the button, it always works and shows "connected". But when using the tester facebook account the response.status is always "not_authorized", whatever the tester authorized the app or not. I am really confused about this. Could anybody figure it out for me? Any suggestions will be appreciated.
function Facebook_login() {
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
alert(response.status);
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
var signed_request = response.authResponse.signedRequest;
} else if (response.status == 'not_authorized') {
alert(response.status);
login();
} else {
//cancelled
}
}, true);
}
function login() {
FB.login(function(response) {
if (response.authResponse) {
var uid = response.authResponse.userID;
console.log('Welcome! Fetching your information.... ');
FB.api('/'+uid, function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
// cancelled
}
},{scope:'email,publish_checkins,publish_actions,publish_stream'});
}
I am using JavaScript API to create my app for Facebook. The problem is, it's returning
email = undefined.
I don't know why? And if I use Facebook login/logout button on my app then the alert shows correct email id of the user but I don't want to do that.
What am I missing?
Here is my code:
<p><fb:login-button autologoutlink="true" perms="user_about_me,email"></fb:login-button></p>
<script>
window.fbAsyncInit = function () {
FB.init({ appId: '250180631699888', status: true, cookie: true,
xfbml: true
});
FB.getLoginStatus(function (response) {
if (response.session) {
greet();
}
});
};
(function () {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
} ());
function greet() {
FB.api('/me', function (response) {
alert('Welcome, ' + response.name + "!");
alert('Your email id is : '+ response.email);
});
}
</script>
// https://developers.facebook.com/docs/javascript/reference/FB.api/
// v2.4
FB.api('/me', { locale: 'en_US', fields: 'name, email' },
function(response) {
console.log(response.email);
}
);
here is example how i retrieve user name and e-mail:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
$(function() {
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
});
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
getCurrentUserInfo(response)
} else {
FB.login(function(response) {
if (response.authResponse){
getCurrentUserInfo(response)
} else {
console.log('Auth cancelled.')
}
}, { scope: 'email' });
}
});
function getCurrentUserInfo() {
FB.api('/me', function(userInfo) {
console.log(userInfo.name + ': ' + userInfo.email);
});
}
});
</script>
According to the latest info on the facebook page you should use 'scope' instead of perms.
https://developers.facebook.com/docs/reference/javascript/FB.login/
If you visit
https://developers.facebook.com/tools/console/
and use the fb-api -> user-info example as a starting point, then logout and back in again, it should ask you for email perms and you can see your email being printed. It is done using response.email as you mention in your post.
<button id="fb-login">Login & Permissions</button>
<script>
document.getElementById('fb-login').onclick = function() {
var cb = function(response) {
Log.info('FB.login callback', response);
if (response.status === 'connected') {
Log.info('User logged in');
} else {
Log.info('User is logged out');
}
};
FB.login(cb, { scope: 'email' });
};
</script>
Use this to for extra permission
for more details visit :
https://www.fbrell.com/examples/
In this code i have get user data form facebook and store into my database using ajax
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me?fields=email,name,first_name,last_name', function(response)
{
FB.api(
"/"+response.id+"/picture?height=100",
function (responses) {
//console.log(responses.data.url)
response['profile_pic']=responses.data.url;
$.ajax({
type:"POST",
url:'<?php echo base_url(); ?>'+'home/facebook_get_signup',
data:response,
success:function(res)
{
if(res=='success')
{
window.location='<?php echo base_url(); ?>';
}
if(res=='exists')
{
window.location='<?php echo base_url(); ?>';
}
}
});
}
)
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
// handle the response
}, {scope: 'email,user_likes'});
There are a couple of things wrong with your solution. First of all you are using the old authentication scheme. You should use the new one described here :
https://developers.facebook.com/docs/reference/javascript/
You need to add the oauth:true to your init function, and make sure that your getLoginStatus looks for the new type of response.
When that is said you need to make sure you have the right permissions to see the users e-mail. You can see the required permissions here:
http://developers.facebook.com/docs/reference/api/user/
You get those by using the FB.login function as described by TommyBs in another answer.
Once you have those options you can use the FB.api function to get the e-mail.
I am having a rough time with getting the facebook javascript SDK workflow to work. The requirement was to offer a "login" link, not a facebook icon. To do this, I improvised a bit. So far it works really well but the session is not persisting from page to page. I am figuring that the session gets set after the login method but it doesn't look like that. Here is login / logout which works fine:
function login() {
FB.login(function(response) {
if (response.session) {
FB.api('/me', function(response) {
document.getElementById('socialauth').innerHTML = "<span>Welcome, " + response.name + " </span>"
+ "(logout)";
});
} else {
}
}, { perms: 'email,publish_stream' });
return false;
}
function logout() {
FB.logout(function(response) {
document.getElementById('socialauth').innerHTML = "login";
});
}
And here is what I am testing with which doesn't fire:
$(document).ready(function() {
FB.getLoginStatus(function(response) {
if (response.status == "connected") {
// logged in and connected user, someone you know
alert("ok - 5 seconds has passed");
} else {
// no user session available, someone you dont know
alert("not ok");
}
});
});
I am also trying "javascript:document.cooike" in my URL box and see not evidence of the FB session.
Kindly advise?
You need to make sure your loading the Facebook SDK on each page as well. For a simple solution that uses jQuery check out https://github.com/xocialhost/jquery.xocialCore.js
The first two functions in the plugin deal with initializing FB and authentication.
Using the provided function you'd do something like this:
$(document).ready(function() {
$.xcInitFacebook = function({appId:'yourappid',callback:function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') { alert('logged in'); }
else { alert('not logged in'); }
} });
});