使用来自codeplex的facebook c#sdk获取用户个人资料照片

时间:2011-02-01 05:23:31

标签: facebook-c#-sdk

我正在使用来自codeplex的facebook C#sdk并试图下载用户的个人资料图片。

我知道我可以从这里得到这个:

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

但是此网址随后会张贴到带有实际图片的第二个网址。我如何获得第二个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;

5 个答案:

答案 0 :(得分:14)

你可以让浏览器使用图形api url - graph.facebook.com/UID/picture?type=large为你获取图像,或者使用类似下面的方法来获取缓存的网址

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

答案 1 :(得分: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");

答案 2 :(得分:5)

您实际上并不需要第二个网址。第二个URL只是一个缓存网址。只需使用graph.facebook.com/username/picture?type=large网址即可。较长的缓存网址可能会发生变化,因此无论如何它都不是图像的可靠来源。

答案 3 :(得分:2)

您可以通过以下方式获取图片网址: graph.facebook.com/username?fields=picture

来自codeplex(v.5.0.3)的facebook C#sdk在尝试执行以下操作时会抛出异常: fb.GetAsync(“me / picture”,get_data_callback); 我猜GetAsync不支持检索(二进制)图像数据。

答案 4 :(得分: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