2012-03-19 69 views
1

我想獲取Facebook用戶的用戶ID,我已經有了access_token,並且使用http請求來執行此操作。我簡單的問題是:我希望用戶的ID,但我的計劃只是崩潰...我使用WPF,C#這是我的一點電話:用http請求獲取Facebook用戶的用戶ID C#WPF

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); 

謝謝!

+0

我想你將要使用的TextReader將得到響應流中的文本或使用像RestSharp庫來撥打電話,並完成所有的工作,爲你。 – 2012-03-19 18:53:14

+0

順便說一句,請務必在'GET'或'POST'的一個地方保留'access_token'。使用'POST'方法從Graph API檢索數據通過'method = get'來表明你需要數據檢索而不是更新。 – 2012-03-19 19:02:16

+0

好吧,我會檢查,但現在我真正的問題是'WebResponse response = req.GetResponse();'不工作... – castors33 2012-03-19 19:02:58

回答

4

不要使用Web瀏覽器!您可以使用HttpWebRequest的那種東西:

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(); 
+0

這是完美的它解決了我的問題...現在我的http請求工作!!!再次感謝 – castors33 2012-03-19 19:17:08