使用带有WPF Web浏览器的facebook C#sdk时出错

时间:2013-04-26 06:50:51

标签: facebook facebook-c#-sdk

我是facebook c#sdk的新手。我按照this link中的教程进行了操作。

我创建了一个在登录后显示用户名的应用程序。这是我的代码:

public partial class MainWindow : Window
{
    private string appId = "appid";
    private string extenededPermissions = "offline_access,publish_stream";
    private Uri loginUrl = null;
    private string accessToken = null;
    private string userName = null;

    public MainWindow()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Function to get the login url
    /// with the requested permissions
    /// </summary>
    private void GetLoginUrl()
    {
        dynamic parameters = new ExpandoObject();
        // add the client id
        parameters.client_id = appId;
        // add the redirect uri
        parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
        // requested response
        parameters.response_type = "token";
        // type of display
        parameters.display = "popup";
        // If extended permissions are present
        if (!string.IsNullOrWhiteSpace(extenededPermissions))
            parameters.scope = extenededPermissions;
        // Create the login url
        Facebook fc = new FacebookClient();
        loginUrl = fc.GetLoginUrl(parameters);
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        // get the login url
        GetLoginUrl();
        // Navigate to that page
        webBrowser.Navigate(loginUrl);
    }

    private void webBrowser_Navigated(object sender, NavigationEventArgs e)
    {
        var fc = new FacebookClient();
        FacebookOAuthResult fr;
        // Check the returned url
        if (fc.TryParseOAuthCallbackUrl(e.Uri, out fr))
        {
            // check if authentication is success or not
            if (fr.IsSuccess)
            {
                getUserName(out userName);
            }
            else
            {
                var errorDes = fr.ErrorDescription;
                var errorReason = fr.ErrorReason;
            }
        }
        else
        {

        }
    }
    private void getUserName(out string name)
    {
        var fb = new FacebookClient(accessToken);
        // Get the user details
        dynamic result = fb.Get("me");
        // Get the user name
        name = result.name;
        MessageBox.Show("Hai " + name + ",Welcome to my App");
    }

}

我的问题在于FacebookOAuthResult

    private void webBrowser_Navigated(object sender, NavigationEventArgs e)
    {
        var fc = new FacebookClient();
        FacebookOAuthResult fr;
        // Check the returned url
        if (fc.TryParseOAuthCallbackUrl(e.Uri, out fr))
        {
            // check if authentication is success or not
            if (fr.IsSuccess)
            {
                getUserName(out userName);
            }
            else
            {
                var errorDes = fr.ErrorDescription;
                var errorReason = fr.ErrorReason;
            }
        }
        else
        {

        }
    }

登录后,重定向到redirect_uri。但是fc.TryParseOAuthCallbackUrl(e.Uri, out fr)失败,尽管webbrowser重定向到Authentication success页面。

所以我无法获得访问令牌。我的代码中的问题是什么?

2 个答案:

答案 0 :(得分:1)

这不回答问题,但我发现你要求获得offline_access权限。 Facebook前一段时间删除了offline_access。相反,您需要一个扩展访问令牌。你通过交换你想要获得的访问令牌来获得它,用于扩展。它们持续约2-3个月,之后你必须得到一个新的。

答案 1 :(得分:0)

没关系我找到了解决方案。感谢question的答案!

我已将 Winforms网络浏览器控件添加到wpf并且身份验证正在运行。问题在于 WPF网络浏览器。它只是省略#token 之后的url所以parseurl将无法对其进行身份验证。

这是经过修改的代码..

       private void WindowLoaded(object sender, RoutedEventArgs e)
       {
          // create the windows form host
        System.Windows.Forms.Integration.WindowsFormsHost sample = 
                       new System.Windows.Forms.Integration.WindowsFormsHost();
        // create a new web browser
        webBrowser = new System.Windows.Forms.WebBrowser();
        // add it to winforms
        sample.Child = webBrowser;
        // add it to wpf
        canvas1.Children.Add(sample);

        webBrowser.Navigated += webBrowser_Navigated;
        webBrowser.Navigate(loginURL);
       }

     void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
      {
         // do the authentication
         var fc = new FacebookClient();
         FacebookOAuthResult fr;
         // Check the returned url
         if (fc.TryParseOAuthCallbackUrl(e.Url, out fr))
         {
            // check if authentication is success or not
            if (fr.IsSuccess)
            {
                accessToken = fr.AccessToken;
                // Actions to do

            }
            else
            {
                var errordes = fr.ErrorDescription;
                var errorreason = fr.ErrorReason;
            }
         }
         else
         {
             //Not a valid url
         }
      }

问题解决了!!