【问题标题】:Dotnetopenauth, retrieve email from facebook scopeDotnetopenauth,从 facebook 范围检索电子邮件
【发布时间】:2012-01-11 15:40:40
【问题描述】:

有一个问题,我在四处搜索时竟然找不到答案。

如果我向 Facebook 请求用户的电子邮件,例如:

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

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

【问题讨论】:

    标签: asp.net facebook json dotnetopenauth


    【解决方案1】:

    据我所知,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/找到可用的字段

    【讨论】:

    • 感谢这个方法对我来说很完美。 :) 我不想再次调用 facebook 服务来获取电子邮件地址。在 DotNetOpenAuth 3.5 库中,FacebookGraph.deserialize 方法不包含 email 属性。
    【解决方案2】:

    使用 DNOA This answer 为我做到了。

    刚刚添加了以下内容:

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

    以及以下到 facebook 图表。

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

    【讨论】:

      【解决方案3】:

      您在上面写的内容似乎是请求用户授权,以允许您的应用在您查询用户的对象时收到电子邮件。要查询用户的对象,请在https://graph.facebook.com/me 上执行 HTTP Get。在 https://developers.facebook.com/tools/explorer 的 Graph API 浏览器工具中试用它

      【讨论】:

      • 其实这就是我获取其他信息的方式: var request = WebRequest.Create("graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); var response = request.GetResponse(); var responseStream = response.GetResponseStream(); var graph = FacebookGraph.Deserialize(responseStream);然后我通过graph.Id收集例如Id。据我所知,虽然图表不包含例如电子邮件。所以我的问题是如何使用 dotnetopenauth 最好地获取这些范围属性。
      • 啊,忘了,你需要通过字段参数指定电子邮件字段,例如:http://graph.facebook.com/me?fields=email
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多