Issue with posting on business page of facebook using c# sdk - c#

I have read many many articles tonight and still do not understood what I shoud do to post on business page of facebook. Here is my code:
string token = getHtml("https://graph.facebook.com/oauth/access_token?client_id=clientID&client_secret=secretKey&grant_type=client_credentials");
token = token.Replace("access_token=", "");
FacebookClient fbClient = new FacebookClient(token);
var args = new Dictionary<string, object>();
args["message"] = "Testing 12qwe3";
fbClient.Post("/pageID/feed", args);
It always says app not authorized. How to authorize it? I read so many articles, but still not able to do it.
I am posting this after totally failing from researching many articles. Kindly let me know whats wrong?
----UPDATE
I did the first part, by making myself the manager page and then creating an app and then providing that app the ability to manage pages and publish etc...but again another issue when i do so even using the above code i still get the same error.But when i use the tools (GRAPH API EXPLORER) portion of facebook site and select this app and generate a token .I than use that directly in app and it posts fine. Kinldy let me know whats wrong!

Related

Custom blazor server-side authentication

I am new to blazor and stumbled across a problem when trying to implement authentication in my app. For now, I need to store all my users' data in JSON file and do not have a SQL DB to refer to, which, as I understand is needed to implement authentication.
As for now I have a list of users and check if the user is in my list of signed in accounts, then call markAsAuthenticated
public void markUserAsAuthenticated(string emailAddress)
{
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, emailAddress),
}, "someType");
var user = new ClaimsPrincipal(identity);
NotifyAuthenticationStateChanged (Task.FromResult(new AuthenticationState(user)));
}
I would like to add roles or claims now to be able to include them in the component, but find it difficult to understand, what the following steps should be.
Most of the tutorials use Authorization services provided by VisualStudio, but we work in VSCode and I would like to implement this myself, so I can use the list of users I have.
I would also like to find out if I can use cookies or JWT in my app and if there are any good tutorials for that or suggestions considering work with VSCode and server-side work, thanks in advance!
If you want to use cookies you need to use a razor page (.cshtml). You can have a razor page inside a Blazor application. It's the same code you'd use in a traditional razor application (pre-Blazor).
There's nothing Visual Studio specific to this that I know of.
See https://blazorhelpwebsite.com/ViewBlogPost/36

Retrieve access token from serverside code

I am new to developing against Facebook. I need to be able to post to other people's walls from my website, which is c#.NET and asp.net. I've already registered a facebook app to do this.
When I press a button on my website, I want to share content to a list of my own users that have provided me with their facebook URLs. They will have granted the FB app permission to their wall.
There's not a lot of information on how to do this. I need to:
Retrieve a new accesstoken, server side, using Facebook SDK.
Given a facebook url, get a user's profile
Share a link to their wall.
i used php and javascript to connect FB SDK. i don't know it can implement to c#
you can get accessToken by FB.getLoginStatus
developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
you can get some permission for grant what you want by "scope" parameter (look about Graph API)
http://developers.facebook.com/docs/getting-started/graphapi/
share some text to wall(feed) see "Publishing" at Graph API
http://developers.facebook.com/docs/reference/api/publishing/
i hope it help you.
sorry for my English skill.
first download the nuget package or dll from https://github.com/barans/FacebookCsharpSdk
fill the appid and secret from www.facebook.com/developers
var config = new Dictionary<string, object>();
config.Add("appId", "3955.......");
config.Add("secret", "4c1d...............");
config.Add("fileUpload", true); //optional
FacebookClient client = new FacebookClient(config);
this code retrieves the access token
client.getAccessToken();
this code retrieves the current user information which is publicly available. you can replace me with a facebook id like "999999"
client.api("/me", "GET", null));
facebook does not allow server side wall posting to an arbitrary user. you can only post to the current user's wall and first you should ask publish_stream permission. after getting that this piece of code makes a wall post
request.Method = FacebookApiMethodType.POST;
request.Path = "/me/feed";
request.Params = new NameValueCollection();
request.Params.Add("link", "www.arcademonk.com");
request.Params.Add("message", "C# SDK Batch Request Messsage");
you can find the all documentation on github page. hope it helps.

Kinda confused on how to use Google Api. Stuck with Oauth

I am not sure if there any libraries(if anyone knows any please let me know) out there that make it easier to use googles api. I know they have classes for .net but they don't seem to work with Windows Phone 7.
I am trying to figure out how it all works but I really find Google's documentation confusion and missing lots of steps.
I decided to try to at first to use their library in an asp.net mvc application before trying to figure out how to do it all with REST requests in a WP7 app.
I am stuck at trying to figure out how to do the Oauth to validation user so I can get their contacts.
RequestSettings settings = new RequestSettings("<var>Test</var>");
settings.OAuth2Parameters = new OAuth2Parameters
{
ClientId = "",
ClientSecret = "",
};
// Need some Code here to validation user so I can then get their contacts.
ContactsRequest cr = new ContactsRequest(settings);
var c = cr.GetGroups();
There is an OAuth NuGet package (DotNetOpenAuth) available, providing authentication through Google, Twitter, Facebook, MSN and possibly more.
Maybe you have more luck with that.

Facebook C# SDK Fan Page Post From ASP.NET

I know this has been asked many times and I apologize if the answer is readily available but I have not been able to find the answer.
I'm using VS 2010 with the newest version of Facebook C# SDK. I installed it using the "Install-Package Facebook" command from my package manager in VS.
After spending quite a bit of time researching how to use the SDK, I am authenticating with FB through the Facebook Javascript SDK. Actually I followed the steps from the Getting Started Tutorial. And was quite successful in my endeavors. I now have a "Login" button on my aspx page, which brings up the login window and allows me to authenticate via that. I was also able to store my access token in a session variable and have it for use.
I have tested this by doing a Get on my FacebookClient object that I created with my access token and was able to retrieve data about the Fan Page.
So having said all this, in order to let you know that I have had some success. I was ready to start posting to the Fan Page via my web site and found this link Simplest way to post to a facebook fan page's wall with C#!
I thought - great! This is exactly what I need. After copying the code from this post, I am now getting an "Unknown Type" on the ExpandoObject.
For the sake of convenience, here is the code that I copied.
if (Session["AccessToken"] != null)
{
var accessToken = Session["AccessToken"].ToString();
dynamic messagePost = new ExpandoObject();
messagePost.access_token = "[YOUR_ACCESS_TOKEN]";
messagePost.picture = "[A_PICTURE]";
messagePost.link = "[SOME_LINK]";
messagePost.name = "[SOME_NAME]";
messagePost.caption = "{*actor*} " + "[YOUR_MESSAGE]"; //<---{*actor*} is the user (i.e.: Aaron)
messagePost.description = "[SOME_DESCRIPTION]";
FacebookClient app = new FacebookClient(accessToken);
app.Post("/" + [PAGE ID] + "/feed", messagePost);
}
So my questions are really about the ExpandoObject. Is this object still in the SDK? If so, what am I missing and if not, how should I be making this call?
And while I'm asking, is there something else I need to do when requesting the access token in order to get the proper permissions to post on the Fan Page? As stated above, I followed the Getting Started page on Facebook C# SDK Site to get my access token. The reason I ask is because throughout my research I have been coming across different posts talking about making sure you have the proper permissions in order to do different things in Facebook.
Any help is greatly appreciated,
Dan
I guess I just needed to do a little more poking around in StackOverflow to find my answer. I will go ahead and post it here for anyone else that might be having the same issue.
As it turns out with the latest version of Facebook C# SDK, the way to post anything is by using the FacebookClient object and then calling the Post method on that object. Pretty easy actually. Here is the code that I used.
if (Session["AccessToken"] != null)
{
var accessToken = Session["AccessToken"].ToString();
FacebookClient app = new FacebookClient(accessToken);
dynamic result = app.Post("/[ID]/feed", new Dictionary<string, object> { { "message", "This Post was made from my website" } });
}
Session["AccessToken"] is my authentication token that was obtained from the user logging into Facebook on the web page, and [ID] is the Id of my Fan Page.

Publish content to Facebook

I apologize if this has already been answered, but all the information out there on Facebook publishing is so confusing and conflicting, I haven't been able to get anything to work yet. I'm trying to set up an application that runs on my local server to publish content to my organization's fan page (this will tie in with my WCMS to cross-post content). I believe I want a Facebook Connect application to do this which I've set up properly in Facebook and gotten an application key and secret. Here's the code I'm trying to execute, but each time it's run I get "User has not authorized access" even if I'm just trying to publish to the application wall.
ConnectSession fbSession = new ConnectSession("APP_KEY", "APP_SECRET");
Api fbAPI = new Api(fbSession);
fbAPI.Stream.Publish("hello world");
I've also tried:
fbAPI.Stream.Publish("hello world", null, null, FAN_PAGE_ID, APP_ID);
I've granted my application access to publish on the fan page.
EDIT:
I've tried turning this into a Desktop application instead because it appears as though Javascript needs to be involved in order for it to be a Connect application. Here's the updated session initialization line. This now gives me a "Incorrect signature error."
DesktopSession fbSession = new DesktopSession("APP_KEY", false);
There is a similar question titled "How can I post to a facebook wall from a c# web service?" for which I provided a very lengthy answer, specifically indicating how to post to a page.
casperOne's solution works, however, I found this article to be the best solution.
http://tech.karolzielinski.com/publish-post-of-facebook-page-wall-as-a-page-not-a-user-python-facebook-rest-api
This authorizes the application to post directly to the Fan Page without having to go through an admin account.

Categories