C#FatClient Facebook身份验证失败:返回URI不包含令牌
在使用C#FatClient Facebook身份验证失败:返回URI不包含令牌,c#,.net,facebook,oauth,authorization,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_frie
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
但我收到的只是
,即没有访问令牌
奇怪的是,将请求URI复制并粘贴到浏览器(例如IE8)中会正确返回身份验证URI
https://www.facebook.com/connect/login_success.html#access_token=[PROPERTOKEN]&expires_in=[PROPERNUMBER]
以下是我一直在尝试的:(全班:)
首先,发送请求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.
}
}
//
///属性来指示与facebook的身份验证是否成功
///
公共bool已成功验证
{
得到
{
//强制转换到浏览器控件以获取当前源
if(uiFrameLogin.Content.GetType()==typeof(WebBrowser))
{
WebBrowser WebBrowser=(WebBrowser)uiFrameLogin.Content;
如果(webBrowser.Source!=null&&webBrowser.Source.ToString()包含(“&error”))
return false;//查找错误
其他的
如果(
webBrowser.Source!=null&&
webBrowser.Source.AbsolutePath.Contains(“登录成功”)
)
{
字符串温度;
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;//如果它位于此页,则表示我们已成功进行身份验证
}
}
return false;//找不到成功页面,不能表示身份验证成功-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);
}
嗨,迈克尔-非常感谢你的建议。请原谅我的无知。。。您还可以给出“webBrowser1”的声明和定义吗?我通过实现一个Webbrowser表单(如中所述)自己解决了这个问题。像糖一样有效。迈克尔。