加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何从.NET发布到Facebook页面墙

发布时间:2020-12-15 08:19:10 所属栏目:百科 来源:网络整理
导读:我创建了Facebook页面. 我没有应用程序密钥,也没有访问令牌. 我想从我的.NET桌面应用程序发布到此页面. 我该怎么做?任何人都可以帮忙,我在哪里可以获得访问令牌? 我应该创建一个新的Facebook应用程序吗?如果是,我如何授予此应用程序的权限以在页面墙上发
我创建了Facebook页面.
我没有应用程序密钥,也没有访问令牌.

我想从我的.NET桌面应用程序发布到此页面.
我该怎么做?任何人都可以帮忙,我在哪里可以获得访问令牌?

我应该创建一个新的Facebook应用程序吗?如果是,我如何授予此应用程序的权限以在页面墙上发布?

UPD1:
我没有网站.
我需要将公司的新闻从.NET桌面应用程序发布到公司的Facebook页面.
我所拥有的只是Facebook页面帐户的登录/密码.

UPD2:
我创建了Facebook应用程序.使用AppID / SecretKey.我可以获得访问令牌.但…
如何授予发布到页面墙的权限?

(OAuthException)(#200)用户尚未授权应用程序执行此操作

解决方法

我已经创建了一个视频教程,展示了如何在这个位置执行此操作:

http://www.markhagan.me/Samples/Grant-Access-And-Post-As-Facebook-User-ASPNet

你会注意到,在我的例子中,我要求“publish_stream”和“manage_pages”.这个让你也发布用户是管理员的页面.这是完整的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Facebook;

namespace FBO
{
    public partial class facebooksync : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {
            CheckAuthorization();
        }

        private void CheckAuthorization()
        {
            string app_id = "374961455917802";
            string app_secret = "9153b340ee604f7917fd57c7ab08b3fa";
            string scope = "publish_stream,manage_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}",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"];

                var client = new FacebookClient(access_token);

                client.Post("/me/feed",new { message = "markhagan.me video tutorial" });
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读