I'm using C# in MonoDevelop with the Graph API to interact with users facebook pages.
I've become thoroughly confused though.
Are these the correct steps?
create facebook app Here : http://developers.facebook.com/
programmaticly get access token using app ID and app Secret.
If so how do I go from this to posting to users walls?
I've done lots of research but have been unable to find anything particularly useful.
I've tried using this ASP.NET Post to Facebook Wall but Nothing ever appears on the wall.
Is there a tutorial or something that can take me through the process nice and slow?
All help or pointers appreciated.
As stated i'm working with unity3d, c# in mono and the facebook graph api.
[Edit] I'd like it to work on android.
Use the C# Facebook library. There is a sample project called facebook-aspnet-sample which shows you authentication and login. Also, have a look at this tutorial page which deals with how to connect from a Unity3d game to Facebook. The tut is short but the demo project download is useful.
There are also plugins for sale that enable Facebook access from Unity3d.
after your have your FB app ready, indeed, you need to get a token. However, token is always requested in the context of a USER - and therefore user should authenticated on the website. That means that Facebook login dialog should be displayed - which will ask user if he indeed trusts the application. There are different ways to do this - here is the generic page about FB authentication: https://developers.facebook.com/docs/authentication/, and this one explains how to do this in Android: https://developers.facebook.com/docs/mobile/android/sso/
if you want to go with JavaScript authentication (which I personally use in MVC3 project), here is a little bit modified example from https://developers.facebook.com/docs/authentication/client-side/ which does this. Remember to change app id and permissions to the ones you need.
<div id="fb-root"></div>
<script>
// 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));
// Init the SDK upon load
window.fbAsyncInit = function () {
FB.init({
appId: '#EventController.FB_APP_ID', // App ID
channelUrl: '//' + window.location.hostname + '/channel.htm', // Path to your Channel File
status: false, // check login status
cookie: false, // enable cookies to allow the server to access the session
xfbml: false // parse XFBML
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function (response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function (me) {
// get info about the user, for example
})
//document.getElementById('auth-loggedout').style.display = 'none';
} else {
// user has not auth'd your app, or is not logged into Facebook
//document.getElementById('auth-loggedout').style.display = 'block';
}
});
// respond to clicks on the login and logout links
document.getElementById('auth-loginlink').addEventListener('click', function () {
FB.login(function (response) {
// handle the response
if (response.authResponse != null) {
// HERE IS THE TOKEN YOU CAN USE
var token = response.authResponse.accessToken;
} else {
}
// AND THESE ARE THE PERMISSIONS YOU WANT YOUR APP TO REQUEST
}, { scope: 'manage_pages,publish_stream' });
});
};
</script>
One catch here - your FB app should have your website URL (even if it is http://localhost/) in App -> Settings -> Website URL field. Also you should put channel.htm file into the root of your website with one line of code in it:
<script src="//connect.facebook.net/en_US/all.js">
Related
I don't understand the documentation of Nopcommerce.
it seems (in the documentation) that it should be very easy steps - http://docs.nopcommerce.com/display/nc/Facebook+Authentication
I have created Facebook app,
I copied and pasted the App ID/API Key
and the App Secret to the nopcommerce admin section : "Configure - Facebook Authentication".
I have put this block inside the body tag of the root:
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'xxxxxxxxxxxxxx',
cookie: true,
xfbml: true,
version: 'v2.2'
});
};
(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'));
</script>
NOW WHAT ???
Where and how should i put the login button?
Facebook authentication doesn't use the JavaScript api. It handles it server side. Follow the steps from the page you linked and those outlined here http://docs.nopcommerce.com/display/nc/External+Authentication++Methods to enable it.
If you still can't see the login with Facebook button, maybe your template its not rendering it. Check the login views from the default theme for more information.
From the external authentication methods configuration screen, make sure you click "edit" then check the box under: "Is Active". Save and you're good to go.
I've seen many questions about this, but most of them are outdated due to Facebook updating their API.
I have a site were people can bind their account with their Facebook account by storing their access token on that user. Which works great.
However the issue is that I have an option were people can connect their Facebook to their account on the spot on a shared computer were people can come and go.
But by authorizing the application on this public computer they'll also get logged into Facebook automatically, so anyone can visit Facebook and thus get into the previous guys Facebook.
But the issue is also that the next guy that comes to bind his account with Facebook will automatically get the previous guys already authorized accesstoken since the already logged in user has already authorized the app.
Is there a way to always force the user authorize their Facebook when they press on the bind Facebook button? Or is there a way to logout the user from Facebook after we have recieved the accessToken?
I have the same issue with Twitter but I will leave that for another question.
Thanks in beforehand.
You don't need to code it. You can find the solution on facebook developers console.
Go to https://developers.facebook.com
Go inside your Application
Go to Facebook Login/Settings at the rigth menu
Set to Yes option Force Web OAuth Reauthentication
That worked fine!
I couldn't find a solution to force people to login(The issue being if they already were logged in & authorized) But I used the javascript SDK to logout the user if he is logged in & authorized while if he is logged in but not authorized or not authorized at all then he'll atleast get the login form
On the connect to Facebook page I have this javascript running that will automatically log out the user that registers & authorizes which means that the next guy that comes to log into his Facebook account will now get a clear login screen
window.fbAsyncInit = function() {
FB.init({
appId: 'AppIdStringHere',
status : true,
xfbml : true
});
FB.getLoginStatus(function (response) {
if (response.status === 'connected')
{
//alert("Authorized");
FB.login(function () {
FB.logout(function (response) {
});
}, { scope: 'publish_actions' });
}
else if (response.status === 'not_authorized')
{
//alert("Logged in but not authorized");
}
else
{
//alert("Not logged in");
}
});
};
(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'));
I have some JavaScript that logs in a Facebook user and saves the access token to a database:
window.fbAsyncInit = function () {
FB.init({
appId: '<%=FaceBookApplicationId() %>',
status: false, // check login status
cookie: true,
oauth: true
});
};
function facebookLogin() {
FB.login(function(response) {
if (response.authResponse) {
__doPostBack('__Page', 'FacebookDeliveryButton: ' + JSON.stringify(response.authResponse));
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'offline_access,read_stream,publish_stream,user_photos' });
}
A button click fires facebookLogin() which logs in a facebook user, getting a facebook session that includes an access token, which I JSON serialize and post to the server. The server then saves this access token to the database table FacebookDeliveryQueue.
I have a Windows service running that periodically queries the FacebookDeliveryQueue table and attempts to post on a user's wall using the access token we saved earlier:
IQueryable<FacebookDeliveryQueue> toSend = objectContext.FacebookDeliveryQueues.Where(p => !p.IsDelivered);
foreach (FacebookDeliveryQueue facebookDeliveryQueueItem in toSend)
{
string facebookAccessToken = facebookDeliveryQueueItem.Cart.FacebookAccessToken;
string facebookRecipientId = facebookDeliveryQueueItem.Cart.FacebookRecipientId;
var client = new FacebookClient(facebookAccessToken);
dynamic parameters = new ExpandoObject();
parameters.message = facebookDeliveryQueueItem.Cart.CustomMessageBody;
client.Post(facebookRecipientId + "/feed", parameters);
}
My problem is, this ONLY works with access tokens from the user that created the facebook application. E.g.
Success:
I, the creator of this application, log in and pick one of my friends to send a message to, this info is saved to the database, the service runs, my message is posted to my friend's wall.
Failure:
I log in on my dummy test account (approving the app permissions on this account), pick one of my dummy test account's friend, this info is saved to the database, the service runs and throws an invalid access token error.
Any ideas why?
Update: Switched to Oauth login -- no change. Still getting "(OAuthException) Invalid access token signature." when attempting to post to friend's wall.
Looks like you're using facebook's old login methods, which they recently just turned off, so your old access tokens aren't valid anymore? And your javascript isn't generating the right kind of token. Read the latest version of the FB.login documentation for more info on what changes you need to make. Specifically,
pass oauth: true to the FB.init call
check for response.authResponse instead of response.session now.
Also, check that your app isn't in "sandbox mode". Go to the app settings page and click on "advanced". Sandbox mode makes it so that only developers can use the app.
The persistence to the database was silently trimming the access token to 75 characters, which in the case of my own user, was enough (small user id because it's an old account) -- but five characters too short in the case of my test account which has a very large user id.
Woops.
I am making a marketing tool that is required to register facebook accounts using web requests. I have been trying to understand the protocol but I always end up with a "Sorry there is something went rong" page. Any explanations are appreciated =].
Regards
edit: I am trying to let the user to register on facebook through my application. He should input the required fields (names,birthday,email) and the application should do the signup process for him after he solves the captcha that will be fetched from facebook. It's not a spam attempt, Don't bother saying you won't answer because it's a spam attempt. If you think so, just don't answer.
edit: I don't want to use any WebBrowser controls, I need it to be through HTTP Requests.
edit: I try to imitate requests sent to facebook from my computer using fiddler, I believe I imitated everything that facebook website does. Though I still getting "Sorry something went wrong" page.
You have several API to to that. I recommend that you use the last one, the graph API. Then you have several SDK, I know PHP and Javascript SDK.
In javascript, with jQuery you can do
<body>
<div>
<button id="login">Login</button>
<button id="logout">Logout</button>
<button id="disconnect">Disconnect</button>
</div>
<div id="user-info"></div>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
// initialize the library with the API key
FB.init({ apiKey: 'YOUR_API_KEY' });
// fetch the status on load
FB.getLoginStatus(handleSessionResponse);
$('#login').bind('click', function() {
FB.login(handleSessionResponse, {
// here you specify the perms your application requires
perms:'publish_stream, offline_access, manage_pages, read_stream'
});
});
$('#logout').bind('click', function() {
FB.logout(handleSessionResponse);
});
$('#disconnect').bind('click', function() {
FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
clearDisplay();
});
});
// no user, clear display
function clearDisplay() {
$('#user-info').hide('fast');
}
// handle a session response from any of the auth related calls
function handleSessionResponse(response) {
// if we dont have a session, just hide the user info
if (!response.session) {
clearDisplay();
return;
}
// if we have a session, query for the user's profile picture and name
FB.api('/me/accounts', function(response) {
$('#user-info').html(JSON.stringify(response));
});
}
</script>
</body>
This example is taken from the documentation of the JavaScript SDK. You need to get you API_KEY from the facebook developper page http://www.facebook.com/developers/apps.php
What is the best way to invite users to an existing event? We'd like to do the following:
create an event, make it public
while on our (external) website give the user a dialog (fb:multi-friend-selector) to select their friends to invite to the event
It's established the new Graph API can't be used (http://bugs.developers.facebook.net/show_bug.cgi?id=10070), but the documentation for the REST API (http://developers.facebook.com/docs/reference/rest/events.invite) implies that it's not possible to invite users to existing events.
If anyone could provide any clarity on this I'd be grateful - reading the vague and contradictory facebook documentation is getting me nowhere.
Specifically, who/where/how should the event be created? - and how can I then invite users to that event from an external website?
This can be done using the Graph API. To use the JavaScript SDK use the following:
FB.api('/[EVENT-ID-HERE]/attending', 'post');
This can only be done once you have an authenticated user, so in full, the code would be:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId: '[APP-ID-HERE]', cookie: true, status: true, xfbml: true
});
FB.Event.subscribe('auth.login', function (response) {
if (response.session) {
FB.api('/[EVENT-ID-HERE]/attending', 'post');
}
});
</script>
<fb:login-button perms="rsvp_event">Attend Event</fb:login-button>