简体   繁体   English

如何使用FacebookClient发布到Facebook页面

[英]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. 我正试图发布到Facebook页面,因为页面和我有问题。

When I run this I get: 当我运行这个时,我得到:

Facebook.FacebookOAuthException: (OAuthException - #200) (#200) User does not have sufficient administrative permission for this action on this page Facebook.FacebookOAuthException:(OAuthException - #200)(#200)用户对此页面上的此操作没有足够的管理权限

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. 如果此代码在Windows服务中并且无法提示用户,我该如何获得此令牌。

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. 好的,我终于可以发布到我的Facebook页面,但现在我需要一个用户这样做。 These are the steps I took... 这些是我采取的步骤......

First I needed to create a new Facebook account that I will use to post to this Page. 首先,我需要创建一个新的Facebook帐户,我将使用该帐户发布到此页面。

Next I went to the Admin Roles section of my Page and added this user as a Manager. 接下来,我转到了我的Page的Admin Roles部分,并将此用户添加为Manager。

I got an Access Token for this user from this URL: 我从这个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 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. 重定向后,URL应包含一个不会过期的access_token。 (expires_in=0 should be in the URL.) (expires_in = 0应该在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. 为了确认这一点,我通过https://developers.facebook.com/tools/debug/access_token访问了Access Token Debugger,输入了我的Access Token并点击Debug。

Finally I made some changes to my code to use my Access Token instead of the application id and application secret. 最后,我对我的代码进行了一些更改,以使用我的访问令牌而不是应用程序ID和应用程序密钥。

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. 此访问令牌现在已硬编码到我的应用程序的配置文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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