获取Facebook页面令牌

时间:2014-08-15 08:04:47

标签: facebook facebook-graph-api access-token

现在我正在从我的私人帐户和我的帐户创建帖子  页。我试图让自己的页面发布我的消息  创建。我一直在阅读关于页面令牌的som文档。但  发现很难找到一些eksampels。你们其中有人吗?  知道下一步发布消息作为一个页面?将会  非常感谢

public void CheckAutorization()
    {
        string app_Id = "my_app_id";
        string app_secret = "my_secret_id";
        string scope = "publish_stream, publish_actions";

        if (Request["code"] == null)
        {
            Response.Redirect(string.Format(
                "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                app_Id, Request.Url.AbsoluteUri, scope));
        }
        else
        {
            Dictionary<string, string> tokens = new Dictionary<string, string>();

            string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                app_Id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());

                string vals = reader.ReadToEnd();

                foreach (string token in vals.Split('&'))
                {

                    tokens.Add(token.Substring(0, token.IndexOf("=")),
                    token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                }
            }




            string access_token = tokens["access_token"];

            var client = new FacebookClient(access_token);
            var myAccount = client.Get("/me/accounts");




            dynamic parameters = new ExpandoObject();
            parameters.message = "test message";
            parameters.link = "http://www.facebook.com";
            parameters.picture = "http://www.fabric.co.uk/wp-content/uploads/2014/06/Facebook-teen-usage.jpg";
            parameters.name = "facebook.fo";

            parameters.caption = "test caption";
            client.Post("/mypageUrl/feed", parameters);
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要发布的页面是页面访问令牌。看看有关如何获取它的Facebook文档:https://developers.facebook.com/docs/facebook-login/access-tokens

基本上,您只需调用/me/accounts即可获取所有页面的访问权限,并使用特定令牌发布而不是用户令牌。

以下是一篇如何在不使用PHP SDK的情况下创建扩展页面访问令牌的文章:http://www.devils-heaven.com/extended-page-access-tokens-curl/