简体   繁体   中英

How to get Facebook profile Picture C# .Net

I need to get the user´s facebook profile picture and input it in a crop structure. I´m using Asp.NET MVC, jcrop and the facebook SDK. Untill now i can input files from my computer. I also have a function that access the facebook of the user and returns a session with the user Id, and a GetPhoto function that should return the profile picture. Can someone help me?

I use this code to input the files from the computer:

[ValidateAntiForgeryToken]
public ActionResult _Upload(IEnumerable<HttpPostedFileBase> files)
{
    if (files == null || !files.Any()) return Json(new { success = false, errorMessage = "No file uploaded." });
    var file = files.FirstOrDefault();  // get ONE only
    if (file == null || !IsImage(file)) return Json(new { success = false, errorMessage = "File is of wrong format." });
    if (file.ContentLength <= 0) return Json(new { success = false, errorMessage = "File cannot be zero length." });
    var webPath = GetTempSavedFilePath(file);
    //mistertommat - 18 Nov '15 - replacing '\' to '//' results in incorrect image url on firefox and IE,
    //                            therefore replacing '\\' to '/' so that a proper web url is returned.            
    return Json(new { success = true, fileName = webPath.Replace("\\", "/") }); // success
}

i tried doing this but the GetPhoto() is returning a null element.

 public ActionResult RetornoFb()
        {
            var _fb = new FacebookClient();
            FacebookOAuthResult oauthResult;

            if (!_fb.TryParseOAuthCallbackUrl(Request.Url, out oauthResult))
            {
            // Error
        }

        if (oauthResult.IsSuccess)
        {
            dynamic parameters = new ExpandoObject();
            parameters.client_id = id;
            parameters.redirect_uri = "http://localhost:4323/Avatar/RetornoFb/";
            parameters.client_secret = secretkey;
            parameters.code = oauthResult.Code;

            dynamic result = _fb.Get("/oauth/access_token", parameters);

            var accessToken = result.access_token;
            Session.Add("FbUserToken", accessToken);
        }
        else
        {
        }

        //return RedirectToAction("Upload");
        HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(GetPhoto());
        var webPath = GetTempSavedFilePath(objFile);

        return Json(new { success = true, fileName = webPath.Replace("\\", "/") }); // success
    }

public byte[] GetPhoto()
        {
            try
            {
                string url = "https://graph.facebook.com/" + GetProfileId() + "?fields=picture.width(480).height(480)";

                WebClient webClient = new WebClient();
                string response = webClient.DownloadString(url);

                dynamic json = JObject.Parse(response);

                string urlPicture = json.picture.data.url;

                return webClient.DownloadData(urlPicture);
            }
            catch (Exception)
            {
                return null;
            }
        }

Resolved changing my GetPhoto Function. I was having permission issues.

private byte[] GetPhoto()
            {
                try
                {
                    var _fb = new FacebookClient(Session["FbuserToken"].ToString());
                    dynamic resultMe = _fb.Get(GetProfileId()+"?fields=picture.width(480).height(480)");

                WebClient webClient = new WebClient();
                string urlPicture = resultMe.picture.data.url;

                return webClient.DownloadData(urlPicture);

            }

            catch (Exception)
            {

                return null;
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM