C# 未通过Facebook登录返回用户电子邮件
我试图通过我的C# 未通过Facebook登录返回用户电子邮件,c#,facebook,winforms,facebook-graph-api,C#,Facebook,Winforms,Facebook Graph Api,我试图通过我的WinForm应用程序中的Facebook登录页面访问用户信息。即使我在扩展权限中请求用户的电子邮件,提交后我得到的只是姓名和id public partial class Form1 : Form { private const string AppId = "MY_APP_ID_IS_HERE"; private Uri loginUrl; private const string extendedPermissions = "public_profil
WinForm
应用程序中的Facebook登录页面访问用户信息。即使我在扩展权限中请求用户的电子邮件
,提交后我得到的只是姓名和id
public partial class Form1 : Form
{
private const string AppId = "MY_APP_ID_IS_HERE";
private Uri loginUrl;
private const string extendedPermissions = "public_profile,user_friends,email";
FacebookClient fbClient = new FacebookClient();
private string _accessToken;
private bool _authorized;
private string _currentName;
private string _currentEmail;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Login();
}
private void Login()
{
dynamic parameters = new ExpandoObject();
parameters.client_id = AppId;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
parameters.response_type = "token";
parameters.display = "popup";
if (!string.IsNullOrWhiteSpace(extendedPermissions))
parameters.scope = extendedPermissions;
var fb = new FacebookClient();
loginUrl = fb.GetLoginUrl(parameters);
webBrowser1.Navigate(loginUrl.AbsoluteUri);
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (webBrowser1.Visible)
{
var fb = new FacebookClient();
FacebookOAuthResult oauthResult;
if (fb.TryParseOAuthCallbackUrl(e.Url, out oauthResult))
{
if (oauthResult.IsSuccess)
{
_accessToken = oauthResult.AccessToken;
_authorized = true;
}
else
{
_accessToken = "";
_authorized = false;
}
if (_authorized)
{
fb = new FacebookClient(_accessToken);
dynamic result = fb.Get("me");
_currentName = result.name;
_currentEmail = result.email;
lblLoggedInUser.Text = string.Format("Facebook User: {0}", _currentName);
lblPassword.Text = string.Format("Email: {0}", _currentEmail);
//Do what need being done now that we are logged in!
}
else
{
MessageBox.Show("Couldn't log into Facebook!", "Login unsuccessful", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
}
当我使用Facebook Graph API Explorer运行查询并询问用户的电子邮件时,它会返回以下调试消息:
显然,我的程序并没有要求用户允许使用其电子邮件。因为在我提交我的登录后,会立即显示一个白色页面,上面有文本“Success”,并且不会返回任何电子邮件值
如何更改登录页面以请求用户的电子邮件?试试看
dynamic result = fb.Get("me?fields=id,name,email");
太神了谢谢托比!今天早上我一直在用这个砸我的头!!