C#FatClient Facebook的身份验证失败:返回URI中不包含令牌(C# FatClie

2019-07-30 03:55发布

接收使用Facebook的OAuth的响应字符串时,我已经遇到一个奇怪的问题System.Windows.Controls.Webbrowser进行身份验证。 下面的请求URI被发送出去。

https://www.facebook.com/dialog/oauth?client_id=[APPID]&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=publish_stream,read_friendlists,email&response_type=token

但我收到的仅仅https://www.facebook.com/connect/login_success.html ,即没有的access_token。

奇怪的是,复制和粘贴的请求URI到浏览器(例如IE8)适当地返回AUTH-URI

https://www.facebook.com/connect/login_success.html#access_token=[PROPERTOKEN]&expires_in=[PROPERNUMBER]

这是我一直在努力:(完整的C#类: http://pastebin.com/GePLHXnD )

首先,发送请求URI:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        StringBuilder authReqUri = new StringBuilder("https://www.facebook.com/dialog/oauth?client_id=");
        authReqUri.Append(Properties.Settings.Default.FBAppID);
        authReqUri.Append(   "&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=");
        authReqUri.Append(Properties.Settings.Default.FBScope);
        authReqUri.Append("&response_type=token");
        Properties.Settings.Default.FBReqString = authReqUri.ToString();
        return;
    }

而onWindowClose执行解析令牌:

    /// <summary>
    /// Property to indicate if authentication with facebook was a success
    /// </summary>
    public bool AuthenticatedSuccessfully
    {
        get
        {
            // Cast to a browser control to get at the current source 
            if (uiFrameLogin.Content.GetType() == typeof(WebBrowser))
            {
                WebBrowser webBrowser = (WebBrowser)uiFrameLogin.Content;
                if (webBrowser.Source != null && webBrowser.Source.ToString().Contains("&error"))
                    return false; // look for an error 
                else
                    if (
                        webBrowser.Source != null &&
                        webBrowser.Source.AbsolutePath.Contains("login_success")
                       )
                    {
                        string temp;
                        temp = Regex.Replace(webBrowser.Source.Fragment, "^.*access_token=", "");
                        Properties.Settings.Default.FBAccessToken = System.Text.RegularExpressions.Regex.Replace(temp, "&.*", "");

                        temp = Regex.Replace(webBrowser.Source.Fragment, "^.*access_token=.*&", "");
                        Properties.Settings.Default.FBExpiresIn = System.Text.RegularExpressions.Regex.Replace(temp, "expires_in=", "");

                        return true; // if its at this page, we've auth'd successfully
                    }
            }

            return false; // cant find the success page, cant indicate a successful auth - no return false.
        }
    }

Answer 1:

此代码的工作....

private void button1_Click(object sender, EventArgs e)
{
  webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
  webBrowser1.Navigate("https://www.facebook.com/dialog/oauth?client_id=[AppId]&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=publish_stream,read_friendlists,email&response_type=token");
}

void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
  Console.WriteLine(webBrowser1.Url);
}


文章来源: C# FatClient Facebook auth fails: Return URI contains no token