C# FatClient Facebook 身份验证失败 : Return URI contains no token

标签 c# .net facebook oauth authorization

我在使用 System.Windows.Controls.Webbrowser 接收 Facebook oauth 响应字符串时遇到了一个奇怪的问题用于身份验证。发送以下请求 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

但是我收到的只是<a href="https://www.facebook.com/connect/login_success.html" rel="noreferrer noopener nofollow">https://www.facebook.com/connect/login_success.html</a> ,即没有 access_token。

FBLoginWindowFail

奇怪的是,将请求 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执行解析token:

    /// <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.
        }
    }

最佳答案

这段代码有效......

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 身份验证失败 : Return URI contains no token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11246578/

相关文章:

c# - WPF 中的高性能绘图控件

iOS - 通过应用程序的 Facebook 打开图共享成功,但在 Facebook 上看起来并不像我想要的那样

java - 开始制作Android应用程序

php - 为什么在IE中使用XFBML注册插件会出现空白?

c# - html数据输入到xml

C# MongoDb 直接从 JSON 运行聚合查询

c# - 使用 SHA1 在 Sql Server 2008 上加密密码

c# - 根据控件的属性查找控件的子元素?

c# - 创建 Windows 桌面应用程序的可执行 exe 文件

.net - Console.WriteLine() 与 Debug.WriteLine() 有什么区别?