c#WPF WebBrowser

时间:2012-08-28 13:02:35

标签: c# wpf facebook

我正在尝试进行简单的Facebook登录。我有一个新窗口,我在其中放置System.Windows.Controls.WebBrowser,然后将其导航到

  

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token

但在我登录后,我将我重定向到

  

https://www.facebook.com/connect/login_success.html

我应该有令牌,但System.Windows.Controls.WebBrowser控件Source属性不会给我完整的URL,给我

https://www.facebook.com/connect/login_success.html代替https://www.facebook.com/connect/login_success.html#access_token=THE_TOKEN&expires_in=7180

2 个答案:

答案 0 :(得分:3)

看看这个answer (MSDN) 我认为它会解决你的问题! 你需要:

<Window x:Class="WpfApplicationTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        Title="Window2" Height="300" Width="300">
     <StackPanel>
        <wfi:WindowsFormsHost>
            <wf:WebBrowser Navigated="WebBrowser_Navigated" x:Name="wfwb"/>
        </wfi:WindowsFormsHost>
     </StackPanel>
</Window>

答案 1 :(得分:0)

只需为c#添加对facebook.dll的引用并制作webbrowser.Navigated事件 - 对我有用

private void m_WebBrowser_Navigated(object sender, NavigationEventArgs e)
    {
        Uri url = e.Uri;
        FacebookOAuthResult result;
        dynamic fb = new FacebookClient();

        if (fb.TryParseOAuthCallbackUrl(url, out result))
        {
            if (result.IsSuccess)
            {
                AccessToken = result.AccessToken;
                Window window = Window.GetWindow(this);
                window.Close();                    
            }
            else
            {
                var errorDescription = result.ErrorDescription;
                var errorReason = result.ErrorReason;
            }
        }
    }