如何在Facebook发布从我的asp.net MVC 3网站 [英] how to post in facebook from my asp.net mvc 3 website

查看:139
本文介绍了如何在Facebook发布从我的asp.net MVC 3网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我pretend整合我的网站与Facebook,whant做出自动职位(在一个特定的Facebook帐户),而用户与Web应用程序进行交互。

I pretend to integrate my website with facebook, whant to make a automatic post (on a specific facebook account) while a user interacts with a web application.

有没有什么办法让像一个Web服务的方式这个操作?认证和调用的URL帖子中,我直接发在Facebook上墙的信息?

is there any way to make this operation like a webservice way?, authenticating and calling a url that posts the information i send directly on the facebook wall?

我使用asp.net MVC3 C#我已经找到了一个Facebook开发工具包库,这是正确的方式来启动或我应该怎么办?

i'm using asp.net mvc3 C# i've found a facebook developer toolkit library, is this the correct way to start or what should i do?

什么是必要只在Facebook帐户自动写个帖子,例如,当我写在我的网站一个新的文章(新闻),它会自动张贴在FB。

What is necessary is only write a post automatically on a facebook account, for example when i write a new article (news) on my website, it will be automatically posted on fb.

任何想法,让我开始?

推荐答案

我做了什么样的相似,当我的MVC应用程序一个共享按钮,用户点击,它张贴在他的墙什么的。使用的OAuth对话框,问题是,它会在浏览器重定向到一个Facebook站点为用户登录并接受该应用程序的权限。

I did something kind of similar, when a user clicks on a "share" button on my mvc app, it posts something on his wall. The problem using the oauth dialog, is that it will redirect the browser to a facebook site for the user to log in and accept the application permissions.

在共享按钮,我把它挂到这个网址:

On the "share" button, I linked it to this url:

                        <a href=""https://www.facebook.com/dialog/oauth?client_id=[YOUR_APP_ID]&redirect_uri=[THE_REDIRECT_PAGE]/&scope=publish_stream"">
                        <img src='@Url.Content("~/Images/facebook_share.png")' alt="Share on Facebook!" style="height:28px" />
                    </a>

YOUR_APP_ID是你的Facebook应用程序ID。
THE_REDIRECT_PAGE是您网站上的公开页面,一旦用户登录并接受的权限,Facebook将自动重定向。当Facebook的重定向,其追加名为code给它的查询参数。
注:该重定向页面必须以/最后,它不能以文件结束,否则它不能正常工作

YOUR_APP_ID is your facebook application ID. THE_REDIRECT_PAGE is a public page on your site that facebook will automatically redirect once the user has logged in and accepted the permissions. When facebook redirects, it appends a querystring parameter called "code" to it. NOTE: The redirect page MUST END with a "/", it cannot end with a document, or else it doesn't work!

一旦用户已经接受了你的要求,你必须问Facebook的另一个code,称为访问code,用于发布用户的墙上。

Once the user has accepted your request, you must ask facebook another code, called access code, used to post on the user's wall.

这code是在重定向网页上:

This code is on the redirect page:

        public ActionResult Index(string code)
    {
        string fbAuthCode = Request["code"]; //The authorization code.
        string fbAppId = "XXXXXXX"; //Your fb application id.
        string fbSecretAppId = "XXXXXXXXXXXXXXXXXXXXX"; //Your fb secret app id, it is found on the fb application configuration page.
        string redirectUrl = string.Format("[THE_REDIRECT_PAGE]", locationPointId, entryLocationId); //The redirect url. THIS MUST BE THE EXACT SAME REDIRECT URL USED ON THE JAVASCRIPT LINK!
        string fbUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + fbAppId + "&redirect_uri=" + redirectUrl + "&client_secret=" + fbSecretAppId + "&code=" + fbAuthCode; //Url used to post.
        string accessToken = string.Empty;

        try
        {
            WebClient client = new WebClient();
            using (Stream stream = client.OpenRead(fbUrl))
            using (StreamReader reader = new StreamReader(stream))
            {
                accessToken = reader.ReadToEnd().Split('&')[0].Replace("access_token=", string.Empty);
                reader.Close();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("An error ocurred while trying to get the fb token in " + fbUrl, ex);
        }

一旦你的访问令牌,就可以发布到用户的墙:

Once you have the access token, you can post to the user wall:

            string postUrl = "https://graph.facebook.com/me/feed";
        string postParameters;

        postParameters = string.Format("message={0}&picture={1}&name={2}&caption={2}&description={3}&link={4}&access_token={5}",
                                            "[Message]",
                                            "[PictureUrl]",
                                            "[Name]",
                                            "[Caption]",
                                            "[Link]",
                                            accessToken);

        try
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(postUrl);

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParameters);
            req.ContentLength = bytes.Length;
            using (System.IO.Stream os = req.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length); //Push it out there
                os.Close();
                using (WebResponse resp = req.GetResponse())
                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                {
                    ViewBag.PostResult = sr.ReadToEnd().Trim();
                    sr.Close();
                }
                os.Close();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("An error ocurred while posting data to the user's wall: " + postUrl + "?" + postParameters, ex);
        }

        return RedirectToAction(XXXXXXXXXXXxx....); //Then i redirect to another page.

您可以看到我扔张贴URL(用于调试目的)除外上。
使用此URL通常可以去Facebook的图形API资源管理器或短绒并检查真正的错误。

You can see that on the exception I throw the posted url (for debugging purposes). With that url you can usually go to the facebook Graph API Explorer or the Linter and check the real error.

我不知道这是不是你想要什么,但希望它给你一个开球...
我挣扎着这几天,因为开放式图形Facebook的文档不是很好吗,至少对我们来说不使用卷曲:)

I don't know if this is exactly what you want but hope it gives you a kickoff... I've struggled a few days with this, because facebook documentation on open graph is not very good yet, at least for us that don't use curl :)

<一个href=\"https://developers.facebook.com/docs/opengraph/tutorial/\">https://developers.facebook.com/docs/opengraph/tutorial/
https://developers.facebook.com/docs/opengraph/

https://developers.facebook.com/docs/opengraph/tutorial/ https://developers.facebook.com/docs/opengraph/

希望它帮助。
MT。

Hope it helps. MT.

这篇关于如何在Facebook发布从我的asp.net MVC 3网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆