2011-02-01 200 views
9

我正在使用來自codeplex的facebook C#sdk並嘗試下載用戶的個人資料圖片。使用facebook獲取用戶個人資料圖片來自codeplex的c#sdk

我知道我可以得到這樣的:

http://graph.facebook.com/UID/picture?type=large

但這URL再上崗與實際畫面的第二URL。我如何獲得第二個網址?在stackoverflow上有一篇關於解析json的帖子,我該怎麼做?

  var app = new FacebookApp(); 
      var me = (IDictionary<string, object>)app.Get("me"); 
      string firstName = (string)me["first_name"]; 
      string lastName = (string)me["last_name"]; 
      string gender = (string)me["gender"]; 
      string email = (string)me["email"]; 
      long facebook_ID = app.UserId; 

回答

14

你可以讓瀏覽器獲取圖像,你使用的graph api url - graph.facebook.com/UID/picture?type=large或者使用類似下面的方法來獲得緩存的url

public static string GetPictureUrl(string faceBookId) 
    { 
     WebResponse response = null; 
     string pictureUrl = string.Empty; 
     try 
     { 
      WebRequest request = WebRequest.Create(string.Format("https://graph.facebook.com/{0}/picture", faceBookId)); 
      response = request.GetResponse(); 
      pictureUrl = response.ResponseUri.ToString(); 
     } 
     catch (Exception ex) 
     { 
      //? handle 
     } 
     finally 
     { 
      if (response != null) response.Close(); 
     } 
     return pictureUrl; 
    } 
5

您實際上並不需要第二個URL。第二個URL只是一個緩存url。只需使用graph.facebook.com/username/picture?type=large網址即可。無論如何,較長的緩存網址可能會發生變化,因此它不是圖像的可靠來源。

8

這裏去它運作良好,我使用它

功能是

private Image getUrlImage(string url) 
     { 
      WebResponse result = null; 
      Image rImage = null; 
      try 
      { 
       WebRequest request = WebRequest.Create(url); 
       result = request.GetResponse(); 
       Stream stream = result.GetResponseStream(); 
       BinaryReader br = new BinaryReader(stream); 
       byte[] rBytes = br.ReadBytes(1000000); 
       br.Close(); 
       result.Close(); 
       MemoryStream imageStream = new MemoryStream(rBytes, 0, rBytes.Length); 
       imageStream.Write(rBytes, 0, rBytes.Length); 
       rImage = Image.FromStream(imageStream, true); 
       imageStream.Close(); 
      } 
      catch (Exception c) 
      { 
       //MessageBox.Show(c.Message); 
      } 
      finally 
      { 
       if (result != null) result.Close(); 
      } 
      return rImage; 

     } 

其呼叫

profilePic = getUrlImage("https://graph.facebook.com/" + me.id + "/picture"); 
+0

什麼是 「me.id」 得到了用戶的圖片的網址嗎?我怎樣才能獲得Facebook的ID? – Kiquenet 2014-08-11 09:01:34

+0

@Kiquenet我是使用getter setter製作的自定義對象,然後用作變量 – 2014-08-19 10:49:40

2

你可以通過這個圖片網址: graph.facebook.com/username?fields=picture

從CodePlex上(v.5.0.3)Facebook的C#SDK拋出試圖做的時候異常像這樣: fb.GetAsync(「me/picture」,get_data_callback); 我猜GetAsync不支持檢索(二進制)圖像數據。

0

您可以通過使用FQL

dim app as new facebookclient(token) 
dim obj as jsonobject = app.get("me/") 
dim fql as string = "Select pic_small, pic_big, pic_square from user where uid = " + CStr(obj("id")) 
dim arr as jsonarray = app.query(fql) 

Facebook FQL page for user table

相關問題