C#实FatClient权威性失败​​:返回URI中不包含令牌 [英] C# FatClient Facebook auth fails: Return URI contains no token

查看:159
本文介绍了C#实FatClient权威性失败​​:返回URI中不包含令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 System.Windows.Controls.Webbrowser 验证接收Facebook的OAuth的响应字符串时,遇到了一个奇怪的问题。继请求URI发送:

<$p$p><$c$c>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

<$p$p><$c$c>https://www.facebook.com/connect/login_success.html#access_token=[PROPERTOKEN]&expires_in=[PROPERNUMBER]

这就是我一直在努力:(全C#类: http://pastebin.com/GePLHXnD

首先,发送请求URI:

 私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
    {
        StringBuilder的authReqUri =新的StringBuilder(https://www.facebook.com/dia​​log/oauth?client_id=);
        authReqUri.Append(Properties.Settings.Default.FBAppID);
        authReqUri.Append(与&amp; REDIRECT_URI = HTTPS://www.facebook.com/connect/login_success.html&范围=);
        authReqUri.Append(Properties.Settings.Default.FBScope);
        authReqUri.Append(与&amp; RESPONSE_TYPE =令牌);
        Properties.Settings.Default.FBReqString = authReqUri.ToString();
        返回;
    }

和onWindowClose执行解析令牌:

  ///&LT;总结&gt;
    ///属性,以指示与Facebook的身份验证是成功的
    ///&LT; /总结&gt;
    公共BOOL AuthenticatedSuccessfully
    {
        得到
        {
            //强制转换为浏览器控件得到的电流源
            如果(uiFrameLogin.Content.GetType()== typeof运算(web浏览器))
            {
                web浏览器网页浏览器=(web浏览器)uiFrameLogin.Content;
                如果(webBrowser.Source = NULL&放大器;!&安培; webBrowser.Source.ToString()包含(与&amp;错误))
                    返回false; //寻找一个错误
                其他
                    如果(
                        webBrowser.Source = NULL&放大器;!&安培;
                        webBrowser.Source.AbsolutePath.Contains(login_success)
                       )
                    {
                        串温度;
                        临时= Regex.Replace(webBrowser.Source.Fragment,^ *的access_token =,);
                        Properties.Settings.Default.FBAccessToken = System.Text.RegularEx pressions.Regex.Replace(温度,与&amp; *,);                        临时= Regex.Replace(webBrowser.Source.Fragment。^ *的access_token = *&放大器;,);
                        Properties.Settings.Default.FBExpiresIn = System.Text.RegularEx pressions.Regex.Replace(温度,expires_in =,);                        返回true; //如果在这个页面,我们已经成功地auth'd
                    }
            }            返回false; //无法找到的成功页面,预示着一个成功的权威性 - 没有返回FALSE。
        }
    }


解决方案

这code工程....

 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
  webBrowser1.Navigated + =新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\");
}无效webBrowser1_Navigated(对象发件人,WebBrowserNavigatedEventArgs E)
{
  Console.WriteLine(webBrowser1.Url);
}

I've been encountering a weird problem when receiving the Facebook oauth response string using System.Windows.Controls.Webbrowser for authentication. Following request URI is sent:

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

but what I receive is only https://www.facebook.com/connect/login_success.html, i.e. no access_token.

Strangely, copy&paste the request URI into a browser (e.g. IE8) properly returns the auth-uri

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

Here's what I've been trying: (Full C# class: http://pastebin.com/GePLHXnD )

First, send the request 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;
    }

And onWindowClose execute parsing the 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.
        }
    }

解决方案

This code works....

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权威性失败​​:返回URI中不包含令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆