4

Have a question I surpsisingly couldnt find an answer to when searching around.

If I request a users email from facebook like:

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

How do I retrieve it? I couldnt find a clear option for this in the FacebookGraph.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Baserz
  • 41
  • 2

3 Answers3

3

Near as I can tell, the FacebookGraph object that is in the examples from DotNetOpenAuth does not support changing the fields that you are receiving. However, since WebRequest that it is prompting is returning a JSON string, you can parse it yourself (or use another JSON parser). That's exactly what I did, using the 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.
    }
}

Note that you specify what fields you want sent back as part of the WebRequest URI. The fields that are available can be found at https://developers.facebook.com/docs/reference/api/user/

Jeffrey Blake
  • 9,659
  • 6
  • 43
  • 65
  • Thanks this method worked perfect for me. :) I do not want to call to the facebook service again for the email address. In DotNetOpenAuth 3.5 library, FacebookGraph.deserialize method does not contain the email property. – Brian Mar 18 '12 at 12:59
2

Using DNOA This answer did it for me.

Just added the following:

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

client.RequestUserAuthorization(scope);

and the following to the facebook graph.

[DataMember(Name = "email")]
public string EMail { get; set; }
Community
  • 1
  • 1
capdragon
  • 14,565
  • 24
  • 107
  • 153
0

What you wrote above appears to be requsting authorization from the user to allow your app to get email back when you query the user's object. To query the user's object you do an HTTP Get on https://graph.facebook.com/me. Try it out in the Graph API explorer tool at https://developers.facebook.com/tools/explorer

DMCS
  • 31,720
  • 14
  • 71
  • 104
  • Well actually this is how i get the other information: var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); var response = request.GetResponse(); var responseStream = response.GetResponseStream(); var graph = FacebookGraph.Deserialize(responseStream); Then i collect for example Id by graph.Id. Though graph do not contain for example email as far as i can see. So my question was how to best get these scope properties using dotnetopenauth. – Baserz Jan 12 '12 at 08:19
  • 1
    Ah, forgot, you need to specify the email field by the fields paramter like: `http://graph.facebook.com/me?fields=email` – DMCS Feb 04 '12 at 21:08