How can I use the Facebook C# SDK to post on Facebook Pages - facebook

I have already requested and gained access to 'manage_pages' and 'publish_stream' permissions from a user. How can I use the c# SDK to get permission to post to one of that user's Business Pages wall (They must select one if they Admin multiple Pages, right?). Once I have that access, what is the best method to use the SDK to post to the Business Page wall?
On a related note, can the C# SDK run under ASP.NET 3.5 effectively? Rackspace Cloud Sites is not allowing .NET 4.0 on production servers yet, so I need to see code examples that don't use the 'dynamic' or 'ExpandoObject' keywords.
Thanks!

following is the code if it helps you
FacebookApp fbApp = new FacebookApp();
FacebookApp app = new FacebookApp(fbApp.Session.AccessToken);
var args = new Dictionary<string, object>();
args["message"] = "abc";
args["caption"] = "This is caption!";
args["description"] = "This is description!";
args["name"] = "This is name!";
args["picture"] = "[your image URL]";
args["link"] = "[your link URL]";
app.Api("/me/feed", args, HttpMethod.Post);
You need to replace /me in app.Api's arguments to the specific id of page.
I'm not sure if its the right way, but it works for wall post of a user :-)

Here's how with c# SDK 5.1.1
FacebookClient fbClient = new FacebookClient(accessToken);
var args = new Dictionary<string, object>();
args["message"] = "Testing 123";
fbClient.Post("/me/feed", args);

args["picture"] = "[your image URL]";
args["link"] = "[your link URL]";
The image url and link won't work if you give a localhost address.You can try by giving url of an image in a remote server.

Related

Facebook SDK Login URL Exposing the Client Secret?

I'm currently building a website where I'll offer the users a link to share a photo on Facebook, whereby the application would:
Provide the user with a link to request permissions from Facebook
Redirect back to the website with the access code from the user's acceptance of the permissions
Server-side post the photo to the user's photos
To generate the link for the first step, I'm doing this:
var fb = new FacebookClient();
var options = new
{
client_id = "MY_APP_ID",
client_secret = "MY_APP_SECRET",
redirect_uri = string.Format("http://localhost:51182/Home/FacebookShare?path={0}", Server.UrlEncode(path)),
response_type = "code",
scope = "publish_stream"
};
var loginUrl = fb.GetLoginUrl(options);
Then this loginUrl value is added to my MVC ViewModel and used in a link in the View.
However, I've noticed that the loginUrl contains the client_secret value in clear text. Isn't this a bad thing? Shouldn't users not be able to see the client_secret? Did I go about this the wrong way?
The fb.GetLoginUrl method is rather simple, in that it just adds anything you give it inside the options object as parameters to the URL created.
So take
client_secret = "MY_APP_SECRET",
out of your options object, and it should not show up in the login URL any more.

Post on Facebook page wall or timeline

As FaceBook launched a new concept of pages, I created a page, but I am not able to find any C# SDK for posting on that page's timeline from code.
First you need C# SDK
you can download from here
http://csharpsdk.org/
may be you need manage_page permission to get a page access token
you can get a page access token using this Graph Request http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Faccounts
after getting page access token you can post a on page wall using this code
var fb = new Facebook.FacebookClient(PageAccessToken);
var postArgs = new Dictionary<string, string>();
postArgs["link"] = "http://www.foo.com";
postArgs["name"] = "Click Here To View";
postArgs["message"] = Message;
fb.Post("[pageid]/feed/", postArgs);

WP7 Facebook integration using C# SDK

I want my game player to able to post game score to his facebook wall from WP7 game. I have gone through following tutorials
1> Tutorial: Logging Into Facebook with Windows Phone 7 (Silverlight) (source code is downloaded from their website.)
PROBLEM: After running sample project, i can login to facebook, but can't figure out how to post the message.
2> I have download sample project from github.
PROBLEM: When i open the project for WP7, a window pop up saying "Solution folder are not supported in this version of application. solution folder '.nudget' will be displayed as unavailable."
I have tried downloading nuget updates as suggested by Prabir's Blog.
3> With this tutorial, i am able to login to facebook.
PROBLEM: unable to post any message. it display inside emulator "The remote server returned an error:NotFound".
please let me know if you find this question inappropriate or lack of research, i will romove question immediately.
I am novice to both WP7 and C#. Please help me to correct above problems.
Thanks in advance
EDIT : Finally got 3rd one working by making small changes in PGLogin.xaml.cs, just change "PRE" to "pre" in "wbLogin_LoadCompleted" method. but still not much satisfy. because its work and sometime don't. it's not stable. and don't know how to logout. any suggestion?
Another user had this exact issue, it was resolved using the following code:
var args = new Dictionary<string, object>();
args["name"] = "Check this out";
args["link"] = "www.xyz.com";
args["caption"] = "";
args["description"] = "description";
args["picture"] = "";
args["message"] = "Check this out";
args["actions"] = "";
FacebookAsyncCallback callBack = new FacebookAsyncCallback(this.postResult);
fbApp.PostAsync("me/feed", args, callBack);
private void postResult(FacebookAsyncResult asyncResult)
{
System.Diagnostics.Debug.WriteLine(asyncResult);
}
Post to Facebook wall using WP7 and Facebook.dll
ShareLinkTask shareLinkTask = new ShareLinkTask();
shareLinkTask.Title = "Divum Photo Browser";
shareLinkTask.LinkUri = new Uri(list_photos.ElementAt(index_).imageUrl, UriKind.Absolute);
shareLinkTask.Show();

How to get AppID based on valid AccessToken and/or Facebook.FacebookClient object

I'm logged in using to my own Application using C# Facebook SDK and I can read/write/delete all data I need.
However for validation purposes I'd like to get AppID that I'm currently using (the one I used when generating login Uri) based on existing valid Access-token and Facebook.FacebookClient object.
In a similar way like in Facebook PHP SDK: $appId = Facebook->getAppId();
My access token is not like string.Concat(appId, "|", appSecret)
If I use Facebook C# Sdk I may use following code to retrieve appID I used when loging in - if at the moment I have in memmory only AccessToken available:
string myAccessToken = "...";
FacebookClient fc = new FacebookClient(myAccessToken);
IDictionary<string, object> resultContainer =
(IDictionary<string, object>)fc.Get("/app");
string retrievedAppID = resultContainer["id"] as string;
The same result can be obtained directly from Graph Api Explorer via:
https://graph.facebook.com/app
If desired Facebook application is selected at top right part of the Graph Api Explorer page.
In the Facebook C# SDK, there is no where that access_token parses(or decoded).
Naturally, Facebook C# SDK api, it takes appId from Web.Config through the FacebookSettings class.
access_token as string property of FacebookSignedRequest class.
So you may follow this link to do it yourself: https://developers.facebook.com/docs/authentication/signed_request/
You should be able to get the AppId like this:
var appId = Facebook.FacebookApplication.Current.AppId;

Post image link with Facebook C# SDK problem

I try to post my image link to Facebook fan page using Facebook C# SDK.
var client = new FacebookClient(this._accessToken);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://photos-f.ak.fbcdn.net/hphotos-ak-snc6/260418_185563101499189_124421460946687_425466_2105002_t.jpg";
parameters.picture = "http://photos-f.ak.fbcdn.net/hphotos-ak-snc6/260418_185563101499189_124421460946687_425466_2105002_t.jpg";
parameters.name = "My Picture";
parameters.caption = "My Picture Caption";
parameters.description = "";
dynamic result = client.Post("{Fan page ID}/feed", parameters);
An error occur like below.
(OAuthException) (#100) FBCDN image is not allowed in stream:
http://photos-f.ak.fbcdn.net/hphotos-ak-snc6/260418_185563101499189_124421460946687_425466_2105002_t.jpg
But if i set "picture" parameter to empty string it work perfectly.
How could i fix this problem?
Facebook doesn't allow you to re-post or hot-link images that are hosted on their CDN. They added this policy and error message back in June 2010. You will need to upload this photo to some kind of non-Facebook server or photo sharing site and then use that url.