问题
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.
}
}
回答1:
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);
}
来源:https://stackoverflow.com/questions/11246578/c-sharp-fatclient-facebook-auth-fails-return-uri-contains-no-token