简体   繁体   English

无法使用asp.net C#编码从facebook api v2.4获取用户电子邮件

[英]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 asp.net c#代码用于获取用户电子邮件,即使我在我的Facebook帐户中检查了应用程序权限,也可以授予访问权限批准的邮件电子邮件是正确的我该怎么办,是否有任何与此相关的Facebook设置来检索我的Facebook应用程序中的用户电子邮件,或者代码中有任何错误

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 1. 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 2.您并未明确要求Graph API v2.4中的email字段
Since v2.4 of the Graph API has been introduced, Facebook changed the default responses of Graph API calls to return less fields. 由于引入了Graph API v2.4,Facebook更改了Graph API调用的默认响应以返回较少的字段。 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. 您可以通过在请求中输入fields参数来完成此操作,例如https://graph.facebook.com/v2.4/me?fields=email,name ,它将返回该用户的名称和电子邮件。

More about the changes of v2.4 can be found in the Graph API changelog . 可以在Graph API changelog中找到有关v2.4更改的更多信息。

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 https://developers.facebook.com/docs/facebook-login/permissions

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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