I can get facebook access token on client side of asp.net mvc application with method FB.getLoginStatus()
The token:
EAAFjVDcBGp4BABSnPzvjeR0ZCqEHExSRIGtvEIMPZCZABh****fk03WZCg10xaPJUs3ZCZAMTnP8
It's set token to cookies. I want to access it on server side, but on server I get it:
Kk9fbM-3d8ih4SpCfMddsj23BW2bqwPlll9LLs8ZYUE.eyJh****IjEwNzI4NDM4NjQ0OTI5MiJ9
Why? What should I do to get token from cookie?
UPDATED
Client code:
function checkLoginState() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected')
return;
FB.login();
});
}
window.fbAsyncInit = function() {
FB.init({
appId: #ConfigurationManager.AppSettings[AppParameterKey.FbApplicationId],
cookie : true,
xfbml : true,
version : 'v2.8'
});
};
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
Server code:
var userToken = HttpContext.Current.Request.Cookies[String.Concat("fbsr_", ConfigurationManager.AppSettings[AppParameterKey.FbApplicationId])].Value
The cookies doesn't contains access_token
After a day I founded solution here:
Problem getting access_token after migrating to OAuth 2.0
I am using FB Javascript SDK to check that a user like my page or not. It's works on localhost but when I upload this on Server the code does not works. My code is below:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'My App Id', // 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) {
var page_id = "My Page ID";
if (response && response.authResponse) {
var user_id = response.authResponse.userID;
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
FB.Data.query(fql_query).wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
alert('Liked');
} else {
alert('Not Liked');
}
});
}
});
};
// 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>
It works on localhost and show an Alert but not on Server
You need to add your internet domain to your application sites whitelist.
Go to http://developers.facebook.com
Open YourApp Settings
Add Platform
Write your Site URL
I am having problem integrating the facebook post to wall for pages (not user).
I have already authorized the app for all the scoop.
Scoop lists:
user_about_me,user_activities,user_birthday,user_checkins,user_education_history,user_events,user_groups,user_hometown,user_interests,user_likes,user_location,user_notes,user_online_presence,user_photo_video_tags,user_photos,user_relationships,user_relationship_details,user_religion_politics,user_status,user_videos,user_website,user_work_history,email,read_friendlists,read_insights,read_mailbox,read_requests,read_stream,xmpp_login,manage_friendlists,manage_notifications,status_update,offline_access,publish_checkins,publish_stream,rsvp_event,manage_pages
I have accepted all grant access scopes and I have successfully connected and get the users data I have save into the database the info of users available pages:
page id and page access token
Whe I am posting a test message It say's : #200) The user hasn't authorized the application to perform this action
here is my login script for fb:
window.fbAsyncInit = function() {
FB.init({
appId:'MY_APP_ID', cookie:true,
status:true, xfbml:true,oauth : true
});
};
(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));
//FB.Event.subscribe("auth.logout", function() {window.location = '/logout'});
$('#facebook_login').click(function(e) {
FB.login(function(response) {
if(response.authResponse) {
parent.location ='http://mywebsite.com/users/oauth_fb';
}
},{scope: 'user_about_me,user_activities,user_birthday,user_checkins,user_education_history,user_events,user_groups,user_hometown,user_interests,user_likes,user_location,user_notes,user_online_presence,user_photo_video_tags,user_photos,user_relationships,user_relationship_details,user_religion_politics,user_status,user_videos,user_website,user_work_history,email,read_friendlists,read_insights,read_mailbox,read_requests,read_stream,xmpp_login,manage_friendlists,manage_notifications,status_update,offline_access,publish_checkins,publish_stream,rsvp_event,manage_pages'});
});
After connecting facebook account I will logout automatically my facebook account with my script:
<script>
window.fbAsyncInit = function() {
FB.init({
appId:'MY_APP_ID', cookie:true,
status:true, xfbml:true,oauth : true
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
FB.logout(function(response) {
window.location = '';
});
}
});
};
(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>
And here is my script for posting (this is for offline posting so user already logged out on facebook):
$oauth_uid = 'PAGE_ID_FROM_DB';
$oauth_token = 'PAGE_TOKEN_FROM_DB'
$post_url = '/'.$oauth_uid.'/feed';
if ($oauth_uid) {
$msg_body = array(
'access_token' => $oauth_token,
'message' => 'tesing post to my page',
);
try {
$postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
How to fixed this issue ? I don't see anything wrong with my script , why I can post to my personal facebook account while I can't post on my facebook page ?
Please help..
Thank you.
This is my implementation of facebook login functionality using Facebook SDK.
<script type="text/javascript">
window.fbAsyncInit = function () {
// Initialize the SDK upon load
FB.init({
appId: '1xxxxxxxxxxxx', // App ID
channelUrl: '//' + window.location.hostname + '//Home/Index',
scope: 'id,name,gender,user_birthday,email,first_name,last_name,access_token',
status: true,
cookie: true,
xfbml: true
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', OnLogin);
};
// This method will be called after the user login into facebook.
function OnLogin(response) {
if (response.authResponse) {
var access_token = FB.getAuthResponse()['accessToken'];
$('#AccessToken').attr('value', access_token);
$("#AuthenticatedWithFacebook").attr("value", true);
FB.api('/me', LoadValues);
}
else {
$("#AuthenticationToken").removeAttr('value');
$("#AuthenticatedWithFacebook").attr("value", false);
}
}
//This method will load the values to the labels
function LoadValues(me) {
if (me.id) {
$("#UserId").attr("value", me.id);
$("#AuthenticationToken").attr("value", "facebook/" + me.id);
$("#FirstName").attr("value", me.first_name);
$("#LastName").attr("value", me.last_name);
$("#EmailAddress").attr("value", me.email);
}
}
</script>
<script>(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#xfbml=1&appId=1xxxxxxxxxxxxxx";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Login Pop-Up works just fine but after logging in I'm getting following exception:
Exception was thrown at line 38, column 184 in http://static.ak.fbcdn.net/rsrc.php/v2/yQ/r/Yax99g-1J46.js
0x80070005 - JavaScript runtime error: Access is denied.
I'm testing here on localhost (http://localhost:59777/) which I have configured in my Facebook application as well.
I am using javascript sdk to update status in facebook. This is my code
window.fbAsyncInit = function() {
FB.init({
appId : 'id', // 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 heres
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 = "http://connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
but i am getting An error occurred message while opening this page.please give a solution to solve this.Or any one else can share the app id which is working fine