I added to phonegap app Facebook Connect,
It works fine and generates Access Token, But when I'm trying to get the userID the value is
"Undenified"
my function:
FB.init({
appId: "************",
nativeInterface:CDV.FB,
xfbml: true,
useCachedDialogs: false,
status: true,
cookie: true,
oauth: true
});
//alert("Init Done");
FB.getLoginStatus(function (response) {
if (response.authResponse) {
if (response.status == 'connected') {
Token = response.authResponse.accessToken;// This one is Good!
userid = response.authResponse.userID;// This one is Bad
}
I use the following and it works fine:
FB.api('/me', {}, function (response) {
fbuserid = response.userid;
});
This gives you the user data. You can also pass the scope just to get what you want like email, userid, etc...
Iterate through the keys to see what is stored
for (var key in response.authResponse) {
console.log(key);
console.log(response.authResponse[key]);
}
Related
I created an app in Facebook app center and wanted to use this code for sending a message to multiple users. I checked my code in Javascript test console and worked properly but when pasted in my page it just showed the first recipient.
<script>
var arr = [];
arr.push(4);
arr.push(5);
FB.getLoginStatus(function (response) {
FB.ui(
{
method: 'send',
to: arr ,
link: "http://en.wikipedia.org/wiki/Apadana",
picture: "http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Takhteh_Jamshid.jpg/200px-Takhteh_Jamshid.jpg",
description:"test me",
display: 'iframe'
},
function (param) {
if (!param.success){
// do smthing
}
else{
//throw Err
}
});
});
And also I initiated by these paramethers:
appid = '370960029693800';
channelurl = 'localhost';
window.fbAsyncInit = function () {
FB.init({
appId: appid,
channelUrl: channelurl,
status: true,
cookie: true,
xfbml: true
});
As I said it works in javascript test console and seems something is wrong in initialization part. Please advise me which part of codes is wrong.
any reason why the below code would not work in IE9 yet work in every other browser i try?:
FB.init({appId: '######', status: true, cookie: true, xfbml: true});
FB.api('/me/likes/######', onMyLikesResponse);
function onMyLikesResponse(response)
{
console.log("length" + response.data.length);
console.log(response);
if(response.data.length==1){
$('#like').show();
}
}
FB.login(function(response) {
if (response.status == 'connected') {
var page_id = "40796308305";
FB.api('/me/likes/'+page_id, function(response) {
if (response.data[0]) {
$('#like').show();
}
});
} else {
// user is not logged in
}
});
I think length will not work on json response. Make sure developer tools are open in IE9, cause you are using console.log.
When I first authorize a user it goes and stays on the Canvas URL(my site) instead of redirecting back to the Canvas Page on facebook.How can I make sure once the user authorizes they go to the Canvas Page?
$(function() {
FB.init({ appId: 'myappid', status: true, cookie: true, xfbml: true, oauth: true });
FB.getLoginStatus(function(response){
if(!response.authResponse){
top.location.href="http://www.facebook.com/dialog/oauth?client_id=myid&redirect_uri=http://mysite/myapp"
}else{
FB.api('/me',function(response){
uname = response.name;
userid = response.id;
$.post("signin.php", { name:uname,id:userid},
function(data) {
});
});
}
});
});
In your code, change the redirect_uri value from: http://mysite/myapp to: http://apps.facebook.com/appnamespace
This is driving me crazy. A user logs into my Facebook app, does some stuff. Then goes to Facebook and logs out. I've got a little timer that calls FB.getLoginStatus() every once in a while to see if the user is still logged in. But whenever FB.getLoginStatus() gets called it returns a response with a session object. WTF? Shouldn't it return undefined/unknown?
I'm using the absolute most basic call to Facebook evar:
FB.init({
appId: 'MY APP ID',
cookies: false,
status: true,
xfbml: false
});
FB.getLoginStatus(function (response) {
if (response.session) {
console.info("Session exists");
} else {
console.info("Session empty");
}
});
setInterval(function () {
FB.getLoginStatus(function (response) {
if (response.session) {
console.info("Session exists");
} else {
console.info("Session empty");
}
});
}, 10000);
I checked and double checked the permissions that are being requested with the allow. I am only requesting email and sms. So.... any advice?
don't do it in the hard way. This situation has a simple solution, just add a "true" statement as a second parameter from the getLoginStatus method:
FB.getLoginStatus(function (response) {
if (response === undefined || response.status === 'connected') {
// Do something after logout
}
}, true);
Just be careful and aware that every call to this function will make your App to go to Facebook's servers and can give you a considerable amount of network load depending on your implementation. Hope this helps!
I had the same problem using your code.
The only way I got round it so far is to initialise the sdk each time login status is queried:
setInterval(function () {
FB.init({
appId : 'xxxx', // App ID
channelUrl : 'xxxx', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.getLoginStatus(function (response) {
if (response.authResponse) {
console.info("Session exists");
} else {
console.info("Session empty");
}
});
}, 10000);
In this way I always get the correct session status. If there's a more efficient way to do this please let me know!
Try initializating FB with
status: false
So it will work as you expect. It worked for me
Are you sure the session always gets destroyed when the user logs out? Maybe it's just a var inside the session that changes if the users logs in or out....?
How do I implement a Facebook authorization? I have no idea where to start. I have seen numerous examples in PHP but none in C#.
Start here: http://developers.facebook.com
If you can use the Facebook javascript sdk (Much easier IMO, and you have asp.net as a tag so i am making assumptions) you could try something like this:
//Facebook iFrame include
window.fbAsyncInit = function () {
FB.init({ appId: YourID, status: true, cookie: true, xfbml: true });
FB.Canvas.setAutoResize();
authorize();
}
/*
* Facebook Authorization
*/
function authorize (){
FB.getLoginStatus(function (response) {
if (response.session) {
// logged in and connected user, carry on
} else {
// no user session available, Lets ask for perms
FB.ui(
{
method: 'permissions.request',
perms: your permissions
},
function (response) {
if (response && response.session != null) {
//User accepted permissions
} else {
//User did not accept permissions
}
});
}
});
}