castors33
castors33

Reputation: 487

get a userID of a facebook user with http request C# WPF

I'm trying to get the userID of a Facebook user, I already have the access_token and I use http request to do it. My simple problem is that : I want the user's id but my program just crash... I use WPF, C# Here is my little call :

var url = string.Format("https://graph.facebook.com/me?access_token=" + token + "&response_type=id");

        var req = WebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

        string postData = "'access_token='" + token;

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        var stream = req.GetRequestStream();
        stream.Write(byteArray, 0, byteArray.Length);
        stream.Close();

        WebResponse response = req.GetResponse();
        aTextBox.Text = ((HttpWebResponse)response).StatusDescription;
        stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);

Thanks!

Upvotes: 1

Views: 1887

Answers (1)

Juicy Scripter
Juicy Scripter

Reputation: 25918

Don't use Web Browser for this! You may use HttpWebRequest for that kind of things:

string url = "https://graph.facebook.com/me?access_token=" + token + "&response_type=id";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

string responseData = readStream.ReadToEnd();

Upvotes: 4

Related Questions