2

I am trying to access user's information through a Facebook Login page in my WinForm application. Even though I ask for user's email in extended permissions, all I get after submit is the name and the 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);
                }
            }
        }
    }

}

When I run the query and ask for the user's email using Facebook Graph API Explorer, it returns this debug message: enter image description here

Apparently my program is not asking for permission to use user's email. Because immediately after I submit my login a white page with the text "Success" is shown and no value for email is returned.

How can I change my login page to ask for user's email?

disasterkid
  • 6,948
  • 25
  • 94
  • 179

1 Answers1

7

Try

dynamic result = fb.Get("me?fields=id,name,email");
Tobi
  • 31,405
  • 8
  • 58
  • 90