2011-02-01 157 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

相关问题