使用c#winform

时间:2016-07-25 11:10:55

标签: c# winforms facebook-graph-api facebook-page facebook-fan-page

The reported error occurs while i am trying to login into the facebook(web browser in winforms) to get the user access token我使用下面的代码将文本框内容发布到我的Facebook墙上,它的工作正常。

private void btn_Post_Click(object sender, EventArgs e)
        {
            string appID, appSecret, userId;
            appID = ConfigurationManager.AppSettings["AppID"].ToString();
            appSecret=ConfigurationManager.AppSettings["AppSecret"].ToString();
            userId = ConfigurationManager.AppSettings["UserID"].ToString();
            var fb = new FacebookClient();
            dynamic result = fb.Get("oauth/access_token", new
            {
                client_id = appID,
                client_secret = appSecret,                    
                grant_type = "client_credentials"
            });
            fb.AccessToken = result.access_token;
            PostToWall(txt_status.Text, userId, fb.AccessToken);
        }

        private static void PostToWall(string message, string userId, string wallAccessToken)
        {
            try
            {
                var fb = new FacebookClient(wallAccessToken);
                string url = string.Format("{0}/{1}", userId, "feed");
                var argList = new Dictionary<string, object>();
                argList["message"] = message;
                fb.Post(url, argList);
                MessageBox.Show("Posted");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message);
            }
        }

然后我创建了facebook页面并试图在其上发布。为此我也启用了管理页面权限,但我无法获取页面访问令牌。 如何能够通过C#代码动态获取Facebook页面访问令牌以在其上发布。

1 个答案:

答案 0 :(得分:0)

有两个API端点可以获取页面令牌:

获取包含页面标记的所有页面列表

def getLetter():
    while True:
        letter = raw_input('Please enter a letter...')

        if letter.isdigit() == True:
            print('That is a number!')
            continue

        if len(str(letter)) >1:
            print("Sorry, you have to enter exactly ONE letter")
            continue

        return letter

获取特定页面的页面令牌:

/me/accounts?fields=name,access_token

如果您想使用令牌发布为网页,请务必使用/page-id?fields=access_token manage_pages进行授权。