I am trying to post a comment from code behind. Is there a way to do this?
I can get the users profile pic by
http://graph.facebook.com/" + fbId + "/picture;
Can I post to a wall in a similar way?
Thnaks
Using facebook custom application and Facebook SDK
var facebookClient = new FacebookClient(FbConf.testimonialsAppId, FbConf.testimonialsAppSecret);
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("message","message on the wall"));
parameters.Add("created_time", DateTime.Now.ToString());
dynamic result = facebookClient.Post(string.Format("{0}/feed", FbConf.testimonialsAppId), parameters);
var id = result.id;
Related
im very new with Facebook apps and read several threads for creating them, but I have some problems with it.
First of all what I want to do: I want to create a web application that is able to post pictures, text and links on a facebook page that is managed by me.
I used the Facebook C# SDK: here!
What I have:
string facebookPageId = "<my page id>";
string app_id = "<my app id>";
string app_secret = "<my app secret>";
string scope = "publish_stream,manage_pages";
var fb = new FacebookClient();
dynamic res = fb.Get("oauth/access_token", new
{
client_id = app_id,
client_secret = app_secret,
grant_type = "client_credentials"
});
var access_token = res.access_token;
dynamic messagePost = new ExpandoObject();
messagePost.access_token = access_token;
messagePost.link = "http://www.test.at";
messagePost.name = "Testbot";
messagePost.caption = "{*actor*} " + "hello this is a test";
messagePost.description = "[SOME_DESCRIPTION]";
FacebookClient app = new FacebookClient(access_token);
app.AppId = app_id;
app.AppSecret = app_secret;
try
{
var result = app.Post("/hrechttest" + "/feed", messagePost);
}
catch (Exception e)
{
}
Well the code runs without any exceptions but in the output window I get the following:
Exception thrown: 'Facebook.FacebookOAuthException' in Facebook.dll
The next problem is:
As I understood it you must link your facebook app with your facebook page, but when I want to do that I cant select the page:
So what I did wrong or missed?
publish_stream is deprecated since many years, publish_pages is the correct permission to post to a Page (as Page).
API reference: https://developers.facebook.com/docs/graph-api/reference/page/feed#publish
Make sure you are using a Page Token, not a User Token:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
How to create Page Apps is explained in the docs too: https://developers.facebook.com/docs/pages/tabs
I have created webapp in .Net MVC which shares post on Facebook. Here is the code of sharing post
var fb = new FacebookClient(facebookAccessToken);
var argList = new Dictionary<string, object>();
argList["name"] = postName;
argList.Add("picture", postImageUrl);
argList["link"] = postUrl;
var actions = new[] { new { name = "More Details", link = morePostDetailsUrl } }.ToList();
argList["actions"] = JsonConvert.SerializeObject(actions);
var postResult = fb.Post("<facebookuserId>/feed", argList);
Now i am able to post this feed successfully but these posts are not being displayed on timeline. I have checked all permission & they are set to public. Also i can see them in activity logs & on Home screen when i logs in.
How to post a feed so that it can be displayed on timeline automatically ?
Also i am using actions, are these actions being deprecated or do i need to submit them for review before going live ?
Please guide me if anyone has some idea on the above.
Thanks
I tried this:
oAuthClient = new FacebookOAuthClient(FacebookApplication.Current);
oAuthClient.RedirectUri = callBack;
var parameters = new Dictionary<string, object>();
parameters.Add("permissions", "offline_access,publish_stream");
var loginUri = oAuthClient.GetLoginUrl(parameters);
this brings me the access token , i save it in Database
now i try to post to facebook doing this :
var postparameters = new Dictionary<string, object>();
postparameters["message"] = tweet;
postparameters["name"] = "This is a name";
_fbClient = new FacebookClient(accessToken);
var result = _fbClient.Post("/me/feed", postparameters);
problem is this tells me that im not authorized to post. so in order to make it work i have to visit this url :
http://www.facebook.com/authorize.php?api_key=[key]&v=1.0&ext_perm=publish_stream
this also have a problem cause this link doesnt redirect back !
so the question is how i do get the access token from the first place with the publish_stream permission?
change
parameters.Add("permissions", "offline_access,publish_stream");
to
parameters.Add("scope", "offline_access,publish_stream");
I'm using Facebook C# sdk with the code,
i'm trying to create a new score for a user
but i get this error:
(OAuthException) An active access token must be used to query information about the current user.
what am i missing?
protected void btnAddScore_Click(object sender, EventArgs e)
{
if (CanvasAuthorizer.Authorize())
{
var fb = new FacebookWebClient();
dynamic parameters = new ExpandoObject();
parameters.score = 77;
parameters.access_token = GetAppAccessToken();
try
{
dynamic id = fb.Post("me/scores", parameters);
lblPostMessageResult.Text = "Message posted successfully";
txtMessage.Text = string.Empty;
}
catch (FacebookApiException ex)
{
lblPostMessageResult.Text = ex.Message;
}
}
}
private string GetAppAccessToken()
{
var oauthClient = new FacebookOAuthClient
{
AppId = FacebookWebContext.Current.Settings.AppId,
AppSecret = FacebookWebContext.Current.Settings.AppSecret
};
dynamic result = oauthClient.GetApplicationAccessToken();
string appAccessToken = result.access_token;
return appAccessToken;
}
edit:
I got the answer form here:
http://facebooksdk.codeplex.com/discussions/279307
the new right code is:
if (CanvasAuthorizer.Authorize())
{
var fb = new FacebookClient(CanvasAuthorizer.FacebookWebRequest.AccessToken);
var oauthClient = new FacebookOAuthClient(FacebookApplication.Current);
dynamic parameters = new ExpandoObject();
parameters.score = 100;
dynamic ac = oauthClient.GetApplicationAccessToken();
parameters.access_token = ac.access_token;
dynamic result = fb.Post(CanvasAuthorizer.FacebookWebRequest.UserId + "/scores", parameters);
}
Answer:-
Actually for using SCORE Graph API you need the "Application access token" which is different than a normal access token
So if you want your task to be done GET an Application access token by using the following script.......
And then replace the generated application_access_token with old access_token, that's it
The below code is written in php try convert it in c# and then apply it
$APPLICATION_ID = "APP_ID";
$APPLICATION_SECRET = "APP_SECRET";
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $APPLICATION_ID .
"&client_secret=" . $APPLICATION_SECRET .
"&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
After getting this application access token you can easily do this task.
When You Need An Application Access Token
You need to use a Facebook application access token when you have a process that acts on behalf of the application, rather than on behalf of a particular user. This happens when you access your Facebook Insights data for your app via the graph, and also when you want to create test Facebook users for your app.
Sadly, the documentation for this is buried in the authentication guide for the Facebook graph API.
Your application need to take "publish_actions" permission from user to update the score.
Refer to Create or update a score for a user section of the below documentation.
https://developers.facebook.com/docs/score/
i searched a lot for an answer to this question but i did not find any helpful resource. I am doing a post on my wall with "https://www.facebook.com/dialog/feed?app_id={0}&display=page&canvas=1&redirect_uri={1}", works just fine, i posted on my wall and as a response i get a post_id in query string at my *redirect_uri* (i do the post with dialog).
Now what i cannot manage to do.. is to obtain the full post information with facebook graph, without using FQL, i found lots of examples of how to get it with FQL Select .. but i realy want to make this just with a Graph call.
I do all these in .net, i tried on the facebook grapk explorer: this is the exact url: http://developers.facebook.com/tools/explorer/266888499999433/?method=GET&path=100002843173627_116703548434417 .
Using the http GET method and having access_token i get the answer i want but doing this from my code i don't succeed. if i do a GET to the http://graph.facebook.com/POST_ID i get an answer as string false... and if i add the ?access_token=MY_ACCESSTOKEN in querystring i get error 400 bad request.
i have a class with this method:
public Facebook.JSONObject GetPostInfo(string postID)
{
//"&access_token=" + this.Token
string url = "http://graph.facebook.com/" + postID ;
string json = this.WebRequest(MyFacebookAPI.oAuthFacebook.Method.GET, url ,String.Empty);
return Facebook.JSONObject.CreateFromString(json);
}
Thanks.
OK.. i finally found the solution:
if (CanvasAuthorizer.Authorize())
{
var fb = new FacebookWebClient();
dynamic parameters = new ExpandoObject();
parameters.message = txtMessage.Text;
try
{
dynamic id = fb.Post("me/feed", parameters);
lblPostMessageResult.Text = "Message posted successfully";
txtMessage.Text = string.Empty;
//get post id
IDictionary<string, object> data = new Dictionary<string, object>();
data.Add("access_token", CanvasAuthorizer.FacebookWebRequest.AccessToken);
dynamic thePost = fb.Get(String.Format("{0}", id.id), data);
string post = String.Format("Post:{0} From: {1} Message: {2}", thePost.id, thePost.from.name, thePost.message);
lblPostMessageResult.Text = Environment.NewLine + post;
}
catch (FacebookApiException ex)
{
lblPostMessageResult.Text = ex.Message;
}
}
where first part makes the post with "message"
and second part we get post with the id we got back from the post.
You need to have extended permissions: publish_stream,read_stream in order for this to work + adding the access_token as parameter into the Get method.
I hope my post will help some people that have troubles getting it right.