Desktop App in C#: Can't get the access token from the embedded webbrowser

∥☆過路亽.° 提交于 2020-01-14 05:10:10

问题


I've created a WPF Desktop Application with C# and placed a System.Windows.Controls.WebBrowser.

Typing this (Where {0} is my app id/key)

https://www.facebook.com/dialog/oauth?&client_id={0}&response_type=token&redirect_uri=https://www.facebook.com/connect/login_success.html&display=popup&scope=publish_stream,offline_access

manually into my firefox/ie/whatever and going to the workflow sends my back to

https://www.facebook.com/connect/login_success.html#access_token=TOKEN

that's great so far.

But navigating my System.Windows.Controls.WebBrowser to the workflow redirects this browser to

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

WITHOUT the access token. What am I doing wrong?


回答1:


I hit something like this while implementing Facebook PowerShell Module. You may be hitting a bug in WPF per http://facebooksdk.codeplex.com/discussions/261528. I had to drop back to WinForms for implementing the login capability only. This also fixed an odd crash-on-exit which I had been experiencing.




回答2:


I've come up with a workaround. The WPF browser cuts off the hash-part of an url, the WinForms webbrowser doesn't.

So watch this code behind of my XAML window which I'm going to use for getting Facebook app permissions from a user:

public partial class DiagnosticBrowserWindow : Window
{
    public DiagnosticBrowserWindow(string urlToRequest)
    {
        InitializeComponent();

        System.Windows.Forms.WebBrowser shadowBrowser = new System.Windows.Forms.WebBrowser();

        shadowBrowser.Navigated += (sender, e) =>
        {
            // the access token is now
            // here in e.Url
        };

        this.Browser.Navigated += (sender, e) =>
        {
            if (this.Browser.Source.AbsoluteUri.StartsWith("https://www.facebook.com/connect/login_success.html"))
            {
                shadowBrowser.Navigate(urlToRequest);
            }
        };

        this.Browser.Navigate(urlToRequest);
    }
}

This is working, because as soon the app permissions have been granted (which we detect by detecting a redirect to login_success.html) we send the shadow browser (which is a WinForms Webbrowser) to the inital request page which is:

https://www.facebook.com/dialog/oauth?&client_id={0}&response_type=token&redirect_uri=https://www.facebook.com/connect/login_success.html&display=popup&scope=publish_stream,offline_access

Facebook will detect, that the permissions already have been granted and send the shadowBrowser back to login_success.html and this time you can read the hash-part.



来源:https://stackoverflow.com/questions/7214731/desktop-app-in-c-cant-get-the-access-token-from-the-embedded-webbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!