Dotnetopenauth,从facebook范围检索电子邮件



有一个问题,我在四处搜索时很惊讶找不到答案。

如果我请求用户从facebook发送电子邮件,如:

var scope = new List<string>();
                scope.Add("email");
                FbClient.RequestUserAuthorization(scope);

我该如何取回它?我在FacebookGraph中找不到一个明确的选项。

据我所知,DotNetOpenAuth示例中的FacebookGraph对象不支持更改您正在接收的字段。但是,由于它所提示的WebRequest返回了一个JSON字符串,您可以自己解析它(或者使用另一个JSON解析器)。这正是我所做的,使用NewtonSoft.Json.dll:

//as part of the uri for the webrequest, include all the fields you want to use
var request = WebRequest.Create("https://graph.facebook.com/me?fields=email,name&access_token=" + Uri.EscapeDataString(authorization.AccessToken));
using (var response = request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        System.IO.StreamReader streamReader = new System.IO.StreamReader(responseStream, true);
        string MyStr = streamReader.ReadToEnd();
        JObject userInfo = JObject.Parse(MyStr);
        //now you can access elements via: 
        // (string)userInfo["name"], userInfo["email"], userInfo["id"], etc.
    }
}

请注意,您指定了要作为WebRequest URI的一部分发送回的字段。可用字段位于https://developers.facebook.com/docs/reference/api/user/

使用DNOA这个答案为我做到了。

刚刚添加了以下内容:

var scope = new List<string>();
scope.Add("email");
client.RequestUserAuthorization(scope);

下面是facebook的图表。

[DataMember(Name = "email")]
public string EMail { get; set; }

您在上面所写的内容似乎是在重新请求用户的授权,以允许您的应用程序在查询用户的对象时返回电子邮件。要查询用户的对象,请在https://graph.facebook.com/me上执行HTTP Get。请在Graph API资源管理器工具中尝试https://developers.facebook.com/tools/explorer

最新更新