如何从我的WP7应用程序向Facebook发布状态更新?

时间:2012-02-10 13:26:47

标签: facebook api windows-phone-7 oauth status

嗨,我正在编写一个应用程序,允许用户从应用程序内发布状态更新,我相信我已经正确登录的身份验证,我只是不知道如何发布状态更新。任何有关如何执行此操作的代码/示例/教程都会非常棒,这是我到目前为止用于身份验证的代码。

public partial class FacebookAuth : PhoneApplicationPage
        {
            private string _accessToken;
            private WebBrowser _webBrowser;

            public FacebookAuth()
            {
                InitializeComponent();
                _webBrowser = new WebBrowser();
                this.Loaded += new RoutedEventHandler(FacebookAuthPage_Loaded);
            }

            void FacebookAuthPage_Loaded(object sender, RoutedEventArgs e)
            {
                //Get this from the facebook
                string appId = "XXXXXXXXXXXXXXX";
                string[] extendedPermissions = new[] { "publish_stream", "offline_access", "user_groups" };

                var oauth = new FacebookOAuthClient { AppId = appId };
                //Telling the Facebook that we want token as response
                //and we are using touch enabled device
                var parameters = new Dictionary<string, object>
                        {
                            { "response_type", "token" },
                            { "display", "touch" }
                        };
                //If there's extended permissions build the string and set it up
                if (extendedPermissions != null && extendedPermissions.Length > 0)
                {
                    var scope = new StringBuilder();
                    scope.Append(string.Join(",", extendedPermissions));
                    parameters["scope"] = scope.ToString();
                }
                //Create the login url
                var loginUrl = oauth.GetLoginUrl(parameters);
                //Add webBrowser to the contentPanel
                ContentPanel.Children.Add(_webBrowser);
                _webBrowser.Navigated += webBrowser_Navigated;
                //Open the facebook login page into the browser
                _webBrowser.Navigate(loginUrl);
            }

            void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
            {
                FacebookOAuthResult result;
                //Try because there might be cases when user input wrong password
                if (FacebookOAuthResult.TryParse(e.Uri.AbsoluteUri, out result))
                {
                    if (result.IsSuccess)
                    {
                        _accessToken = result.AccessToken;
                        MessageBox.Show(_accessToken);
                        //Hide the browser controller
                        _webBrowser.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        var errorDescription = result.ErrorDescription;
                        var errorReason = result.ErrorReason;
                        MessageBox.Show(errorReason + " " + errorDescription);
                    }
                }
            }

            private void PostBtn_Click(object sender, RoutedEventArgs e)
            {

            }
        }
    }

1 个答案:

答案 0 :(得分:2)

要发布状态,请使用名为“message”的帖子参数向/me/feed发送HTTP帖子,其值为验证用户发布的值。

使用FacebookClient

var client = new FacebookClient(user_access_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Hello World!"
dynamic post_id = client.Post("/me/feed", parameters);

另请参阅:http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx