简体   繁体   中英

unable to get user email from facebook api v2.4 using asp.net c# coding

asp.net c# code use to get the user email where grant acess even i check the app permissions in my facebook account approved items email is true what can i do, is there any Facebook setting regarding this to retrieve user email in my Facebook app or there is any bug in the code

using ASPSnippets.FaceBookAPI;
using System.Web.Script.Serialization;

  protected void Login(object sender, EventArgs e)
    {
        FaceBookConnect.Authorize("user_photos,email", Request.Url.AbsoluteUri.Split('?')[0]);
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        FaceBookConnect.API_Key = "753961091398319";
        FaceBookConnect.API_Secret = "secretkey";

            string code = Request.QueryString["code"];
            if (!string.IsNullOrEmpty(code))
            {
                string data = FaceBookConnect.Fetch(code, "me");
                FaceBookUser faceBookUser = new JavaScriptSerializer().Deserialize<FaceBookUser>(data);
                faceBookUser.PictureUrl = string.Format("https://graph.facebook.com/{0}/picture", faceBookUser.Id);



                lblId.Text = faceBookUser.Id;
                lblUserName.Text = faceBookUser.UserName;
                lblName.Text = faceBookUser.Name;
                lblEmail.Text = faceBookUser.Email;
                ProfileImage.ImageUrl = faceBookUser.PictureUrl;
                    btnLogin.Enabled = false;

            }
        }

}
public class FaceBookUser
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public string PictureUrl { get; set; }
    public string Email { get; set; }
}

There can be two possible reasons:

1. There is no valid email address for that account on facebook
This could mean that there simply is no valid email address on file for that user. The reasons for this can vary, it could be that there were emails to this address that bounced and the user never re-verified it.

2. You are not explicitly requesting the email field in v2.4 of the Graph API
Since v2.4 of the Graph API has been introduced, Facebook changed the default responses of Graph API calls to return less fields. This change has been made to reduce data usage etc. (for example for mobile devices). You now have to explicitly request the fields you want (if they're not included in the default response).

You can do this by passing in the fields parameters in your request, for example https://graph.facebook.com/v2.4/me?fields=email,name which would return the name and the email for that user.

More about the changes of v2.4 can be found in the Graph API changelog .

Change

string data = FaceBookConnect.Fetch(code, "me")

to

string data = FaceBookConnect.Fetch(code, "me?fields=email");

the syntax of other variables like UserName, name etc... can be consulted on

https://developers.facebook.com/docs/facebook-login/permissions

Example:

string data = FaceBookConnect.Fetch(code, "me?fields=email,first_name,last_name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM