简体   繁体   中英

How do I post to a Facebook page with FacebookClient

I'm trying to post to a facebook page, as the page and I'm having problems.

When I run this I get:

Facebook.FacebookOAuthException: (OAuthException - #200) (#200) User does not have sufficient administrative permission for this action on this page

I believe I need a User Access Token and not an Application Access Token. Examples online are showing exchanging a "code" for a token. I currently have no way of retrieving this "code".

This process seems incredibly complicated for a such a simple task. Am I missing something here?

Do I need a User Access Token? How can I get this token assuming this code is in a Windows Service and cannot prompt the User.

const string applicationId = "114810611889734";
const string applicationSecret = "*** SECRET ***";
const string pageId = "102661313114041";

var client = new FacebookClient();

dynamic token = client.Get("oauth/access_token", new
{
    client_id = applicationId,
    client_secret = applicationSecret,
    grant_type = "client_credentials"
});

client.AccessToken = token.access_token;

dynamic parameters = new ExpandoObject();
parameters.title = "test title";
parameters.message = "test message";

var result = client.Post(pageId + "/feed", parameters);

Ok, I was finally able to post to my Facebook Page, but now I need a User to do so. These are the steps I took...

First I needed to create a new Facebook account that I will use to post to this Page.

Next I went to the Admin Roles section of my Page and added this user as a Manager.

I got an Access Token for this user from this URL:

https://www.facebook.com/dialog/oauth?client_id=APPLICATION_ID&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=code+token&display=popup&scope=manage_pages

After the redirect, the URL should contain an access_token that does not expire. (expires_in=0 should be in the URL.)

To confirm this I went to the Access Token Debugger at https://developers.facebook.com/tools/debug/access_token , entered my Access Token and hit Debug.

Finally I made some changes to my code to use my Access Token instead of the application id and application secret.

const string accessToken = "MY_ACCESS_TOKEN";
const string pageId = "MY_PAGE_ID";

var client = new FacebookClient(accessToken);

dynamic parameters = new ExpandoObject();
parameters.title = "test title";
parameters.message = "test message";

var result = client.Post(pageId + "/feed", parameters);

This Access Token is now hard coded into the config file of my app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM