在facebook页面上使用c#作为使用graph api的页面

本文关键字:graph api facebook | 更新日期: 2023-09-27 17:49:23

private void CheckAuthorization()
    {
        string app_id = "*****";
        string app_secret = "******";
        string scope = "manage_pages,publish_pages";
        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('&'))
                {
                    //meh.aspx?token1=steve&token2=jake&...
                    tokens.Add(token.Substring(0, token.IndexOf("=")),
                        token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                }
            }
            string access_token = tokens["access_token"];
            //tokens["access_token"];
            var client = new FacebookClient(access_token);
            Dictionary<string, string> data = new Dictionary<string, string>();

            client.Post("/1038145769594318/feed", new
            {
                message = " test page post",
                picture = "http://service.extrabucks.in/EmailTemplateCss/7/images/bottom-background2.png",
                name = "Nikunj Patel",
                link = "www.extrabucks.in",
                description = "Test Description",
                type = "links"
            });
        }
    }

嘿,我使用这个代码在Facebook上张贴使用c#,但问题是,它不是张贴作为一个页面,它的张贴作为一个用户。我想张贴作为一个页面在我的粉丝页。请给我一个解决方案使用上述代码。还有一件事,我想终身工作access_token我该怎么做?提前感谢

在facebook页面上使用c#作为使用graph api的页面

public static string GetPageAccessToken(string userAccessToken)
    {
        FacebookClient fbClient = new FacebookClient();
        fbClient.AppId = "*****";
        fbClient.AppSecret = "**************";
        fbClient.AccessToken = userAccessToken;
        Dictionary<string, object> fbParams = new Dictionary<string, object>();
        JsonObject publishedResponse = fbClient.Get("/me/accounts", fbParams) as JsonObject;
        JArray data = JArray.Parse(publishedResponse["data"].ToString());
        foreach (var account in data)
        {
            if (account["name"].ToString().ToLower().Equals("opening shortly"))
            {
                return account["access_token"].ToString();
            }
        }
        return String.Empty;
    }

这个代码为我工作,每次刷新页面访问令牌

just add

client.Post("/1038145769594318/feed", new
            {
                message = " test page post",
                picture = "",
                name = "Nikunj Patel",
                link = "www.extrabucks.in",
                description = "Test Description",
                type = "photo",
                 access_token = GetPageAccessToken(access_token)
            });