I newbie using Facebook SDK for C#
I have seen this post to wall in unit test.
Get Access Token programmatically in Unit Test Method
Now, I want to delete post in my wall.
using Facebook;
[TestMethod]
public void Post_to_the_wall()
{
var client = new FacebookClient(token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.actions = new
{
name = "View on Zombo",
link = "http://www.zombo.com",
};
parameters.privacy = new
{
value = "ALL_FRIENDS",
};
dynamic result = client.Post("me/feed", parameters);
// TODO: NOW, delete the post ???
}
Any suggestions ?
Simply do this:
dynamic result = client.Post("me/feed", parameters);
client.Delete(result.id);
Post method get Postid (id property), and you can use it for delete the post.
https://developers.facebook.com/docs/graph-api/reference/v2.2/post
An app can delete a post if it published it
Check out the example code on that page. You need a User Token with publish_actions to make a DELETE request to /post-id.
Related
First, I open Facebook Developers page, and I create new App. ( I get AppID, AppSecret values)
I want create Unit Test method for do Post to the wall, and later delete the post.
publish_stream, publish_actions
Manually, I do this 3 steps
Open url
https://graph.facebook.com/oauth/authorize?client_id=xxxxx&redirect_uri=http://www.kiquenet.com/&scope=publish_stream,publish_actions
Then, this url opened, and I get the code value:
http://www.kiquenet.com/?code=A....3qhOw#=
And then, open this url and I get the access token value:
https://graph.facebook.com/oauth/access_token?client_id=xxxxx&redirect_uri=http://www.kiquenet.com/&scope=publish_stream,publish_actions&client_secret=zzzzz&code=A...3qhOw#=
Finally, I get the access token:
const string token = "C...SNo";
My code now is for my unit test is working. Only I need do the delete.
using Facebook;
[TestMethod]
public void Post_to_the_wall()
{
var client = new FacebookClient(token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.actions = new
{
name = "View on Zombo",
link = "http://www.zombo.com",
};
parameters.privacy = new
{
value = "ALL_FRIENDS",
};
dynamic result = client.Post("me/feed", parameters);
// TODO: NOW, delete the post ???
}
How can I do programmatically the 3 Manually steps ?
I have a website made with ASP.NET webform .NET 4.5 C#. This site contains a forum(homemade by me), parts of this forum needs to be posted to the specific facebook wall(made for this webpage). What I need :
Post just created thread(message) from specific part of the forum to the corsponding facebook wall.
Optional : Sync the forum thread on webpage with message/comments on the specific facebook page
I have looked at these guides :
http://www.codeproject.com/Articles/569920/Publish-a-post-on-Facebook-wall-using-Graph-API
http://www.c-sharpcorner.com/UploadFile/raj1979/post-on-facebook-users-wall-using-Asp-Net-C-Sharp/
But im not sure that this is really the solution for me? I have tried to follow the guide but it does not look the same.
Edit :
dynamic result;
//https://developers.facebook.com/tools/explorer/
//https://developers.facebook.com/tools/access_token/
FacebookClient client = new FacebookClient(ConfigurationManager.AppSettings["FacebookAppToken"]);
//result = client.Get("debug_token", new
//{
// input_token = ConfigurationManager.AppSettings["FacebookAppToken"],
// access_token = ConfigurationManager.AppSettings["FacebookAppToken"]
//});
//result = client.Get("oauth/access_token", new
// {
// client_id = ConfigurationManager.AppSettings["FacebookAppId"],
// client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
// grant_type = "client_credentials",
// //redirect_uri = "http://www.MyDomain.net/",
// //code = ""
// });
result = client.Get("oauth/access_token", new
{
client_id = ConfigurationManager.AppSettings["FacebookAppId"],
client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
grant_type = "client_credentials"
});
client.AccessToken = result.access_token;
result = client.Post("[IdOfFacebookPage]/feed", new { message = "Test Message from app" });
//result.id;
result = client.Get("[IdOfFacebookPage]");
return false;
Approach to post to a facebook wall:
You need to register in facebook developer tools.
Create a app (fill a form).
Download FGT 5.0.480 (Facebook graph toolkit)
Reference FGT dlls in your web application.
FGT has methods to post to facebook wall but needs appId.
Use the app id of your app created in step 2.
To post to an user's wall through facebook, it is only possible through an app.
Try this asp .net app, which will allow you to post in your wall:
https://apps.facebook.com/aspdotnetsample/?fb_source=search&ref=br_tf
This will allow you to envision what you need.
Your app gets an appId with which you need to generate auth token.
Limitation: The user's wall where you want to post should have added the app created at step 2, this is compulsory and there is no work around.
When the user accesses the app for the first time, it automatically asks for permission to post to wall--> the user needs to accept it.
Hope this gives you an idea on how to go about doing this.
You can then post into the user's wall.
For Banshee's below request using Facebook SDK for .NET:
userId - facebook user id, wall to post the message
This user should have added the app created by you in step 1 else it will say unauthorized access.
dynamic messagePost = new ExpandoObject();
messagePost.picture = "http://www.stackoverflow.com/test.png";
messagePost.link = "http://www.stackoverflow.com";
messagePost.name = "This is a test name";
messagePost.caption = "CAPTION 1";
messagePost.description = "Test desc";
messagePost.message = "message"
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new {
client_id = "app_id",
client_secret = "app_secret",
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
try
{
var postId = fb.Post(userId + "/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
//handle oauth exception
}
catch (FacebookApiException ex)
{
//handle facebook exception
}
You will need an extended token to publish to a wall.. Steps to get extended token is explained well by Banshee..
Edit: How to get extended token by Banshee:
Please follow this post
By creating a extended page token and use it to make the post everything works just fine. See this : How to get Page Access Token by code?
Im surprised that this simple task was so hard to get running and that there was vary little help to get.
I am using visual studio 2013 .NET 4.5 and C# with a windows forms client application, I am trying to post a message to my own Facebook managed page. I have created a new Facebook application, but not yet published it, I am unsure of the settings, but I have the app_id, app_secret and from debugging I have the access_token.
I used a piece of code to open a browser window to login with permissions and this was successful/
I am getting the following error:
OAuthException - #200) (#200) The user hasn't authorized the application to perform this action
The code I am using is below:
var fb = new FacebookClient("AUTH_CODE")
dynamic parameters = new ExpandoObject();
parameters.message = "hello tweet tweet";
dynamic result = fb.Post("SITE_ID/feed", parameters);
var id = result.id;
for logging in I am using:
var client = new FacebookClient();
client.AppSecret = app_secret;
client.AppId = app_id;
client.AccessToken = "AUTH_CODE";
var fbLoginUri = client.GetLoginUrl(new
{
client_id = app_id,
redirect_uri =
"https://www.facebook.com/connect/login_success.html",
response_type = "code",
scope = "manage_pages,offline_access,publish_stream,publish_actions"
});
System.Diagnostics.Process.Start(url);
I am already logged in and the message on the browser shows SUCCESS...
The DLL that I have referenced is the latest v6.0 Facebook C# SDK
Do I need to setup the Facebook app so that it has permissions to post onto my managed wall and if so how do I do it.
I hope someone out there can help, I have been looking at this for days and I need a little help.
PS:
I have also tried the following code:
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = app_id,
client_secret = app_secret,
grant_type = "client_credentials",
scope = "manage_pages,offline_access,publish_stream"
});
var app_token = result.access_token;
var client = new FacebookClient(app_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.natiska.com/article.html";
parameters.picture = "http://www.natiska.com/dav.png";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
//446533181408238 is my fan page
client.Post("/446533181408238/feed", parameters);
Paul.
It seems that the request code from your access token request is missing which probably causes this error, see this question for more details on this.
I have an MVC4 application that uses OAuth to login via facebook. Once logged in the user can then choose a guestlist from their Facebook Friends. This is fine as I am managing to retrieve the facebook Friends and their ID's.
Now I want to be able to post to the Friends wall a message from the application. Is this possible at th moment as there seems to have been a lot of changes to what Facebook allows you to do, if a wall post is not possible, can a private message be sent?
I have no code snippets from this one, just direction would be appreciated.
Thanks
I have used this code for posting on friend wall
var fbApplication = new DefaultFacebookApplication { AppId = fbapp, AppSecret = fbsec };
var current = new FacebookWebContext(fbApplication);
Facebook.Web.FacebookWebClient client = new Facebook.Web.FacebookWebClient(token);
//client.AccessToken = current.AccessToken;
dynamic parameters = new ExpandoObject();
parameters.message = "";
parameters.link = link;
parameters.name = name;
parameters.caption = caption;
parameters.description = desc;
parameters.from = fromId;//your fb ID
//friendId will be the ID of the person,you wants to post on wall
object resTest = client.Post("/" + friendId + "/feed", parameters);
Alternatively you can use Graph API's feed dialog
check this
My facebook app uses the Facebook C# SDK to publish to a user's Facebook profile. I'm currently publishing multiple posts with one attachment, but I'd much rather publish one summary post with multiple attachments. I've done this with the JavaScript API, but is it possible with the C# SDK?
This is my current publish code:
FacebookApp app = new FacebookApp(user.AccessToken);
string userFeedPath = String.Format("/{0}/feed/", user.FacebookUserId);
string message = String.Format("{0} earned an achievement in {1}",
user.SteamUserId, achievement.Game.Name);
dynamic parameters = new ExpandoObject();
parameters.link = achievement.Game.StatsUrl;
parameters.message = message;
parameters.name = achievement.Name;
parameters.description = achievement.Description;
parameters.picture = achievement.ImageUrl;
app.Api(userFeedPath, parameters, HttpMethod.Post);
We currently don't support multiple attachments. As far as I know you can't publish multiple attachments with either the graph or rest api. If you have a sample that shows how to do it, I will get it implemented in the SDK.
i have the same code as yours but it doesent work for me. I am trying this:
public void plesni()
{
try
{
dynamic parameters = new ExpandoObject();
parameters.message = "xxxxxxx";
parameters.link = "xxxxxxxx";
// parameters.picture=""
parameters.name = "xxxxxx";
parameters.caption = "xxxxxxx";
parameters.description = "xxxxxxxxxx";
parameters.actions = new
{
name = "xxxxxxx",
link = "http://www.xxxxxxxxxxxxxx.com",
};
parameters.privacy = new
{
value = "ALL_FRIENDS",
};
parameters.targeting = new
{
countries = "US",
regions = "6,53",
locales = "6",
};
dynamic result = app.Api("/uid/feed/", parameters, HttpMethod.Post);
// app.Api("/uid/feed", parameters);
Response.Write("Sucess");
}
catch (FacebookOAuthException)
{
Response.Write("...... <br/>");
}
}
if instead of uid I put me it works fine.
I am hoping for your help.
Have a good day.