facebook-c#-sdk:在用户的墙上发布

时间:2011-03-16 10:12:45

标签: facebook-c#-sdk

嗨,我还在学习Facebook C# SDK 5.0.3,我正在构建一个facebook Apps(iFrame)。我按照SDK上附带的示例进行操作。我成功运行它并且我想通过创建一个按钮事件来扩展它,每次用户单击按钮时,“Hello World”将发布到他的墙上。这是代码;

protected void btnRegister_Click(object sender, EventArgs e)
{
    try
    {


        var fbApp = new FacebookClient(FacebookContext.Current);
        dynamic result = fbApp.Post("/me/feed", new Dictionary<string, object> { { "message", "Hello World" } });


    }

    catch (Exception ex)
    {
        Response.Write(ex.ToString());
        Msg.Text = "An error may occurred while processing your request, you may try again.";
    }
}

运行此代码后,我收到此错误

  

(OAuthException)(OAuthException)必须使用活动访问令牌来查询有关当前用户的信息。

请注意,在我的page_load事件中,我可以使用Sdk包上的示例成功获取用户名,

public FacebookSession CurrentSession
{
    get { return (new CanvasAuthorizer()).Session; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Response.Redirect("Registration-Closed.aspx");
        Panel1.Visible = true;
        Panel2.Visible = false;


        var auth = new CanvasAuthorizer { Perms = "user_about_me,publish_stream,offline_access" };

        if (auth.Authorize())
        {
            ShowFacebookContent();
        }

    }
}

我怀疑我没有为我的按钮事件获得正确的访问令牌。

2 个答案:

答案 0 :(得分:2)

我希望我的作品:

var client = new FacebookClient("my_access_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",
};
parameters.targeting = new {
    countries = "US",
    regions = "6,53",
    locales = "6",
};
dynamic result = client.Post("me/feed", parameters);

答案 1 :(得分:0)

普通访问令牌将在某些分钟后过期。我认为您的访问令牌在您调用post事件之前已过期。您可以续订访问令牌然后继续