1

I am building an API and one resource we need to pull through for our portal is the Profile photo from Salesforce. I am not returning an error, but the byte string coming back is not presenting the photo in their profile. It presents an empty picture.

Here is the code I am using.

public async Task<string> GetRepPhoto(string photoId)
{
    var auth = new AuthenticationClient();
    var url = Properties.Settings.Default.url1;
    await auth.UsernamePasswordAsync(Properties.Settings.Default.consumerKey, Properties.Settings.Default.consumerSecret, Properties.Settings.Default.acctName, Properties.Settings.Default.acctPw + Properties.Settings.Default.token, url);
    var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);

    var req = (HttpWebRequest)WebRequest.CreateHttp(auth.InstanceUrl + "/services/data/" + auth.ApiVersion + "/connect/user-profiles/" + photoId + "/photo "); 
    req.Headers.Add("Authorization: Bearer " + auth.AccessToken);
    req.Method = "GET";

    WebResponse resp = req.GetResponse();
    byte[] photo = null;
    using (MemoryStream ms = new MemoryStream())
    {
        resp.GetResponseStream().CopyTo(ms);
        photo = ms.ToArray();
    }

    return Convert.ToBase64String(photo);
}

public async Task<string> GetPhotoUrl(string photoId)
{
    var photoString = await GetRepPhoto(photoId);
    return "data:image/jpeg;base64," + photoString;
}
  • 1
    I once had a similar thing happen. The user had clicked update on their user profile picture, and deselected the Show my photo on publicly accessible pages. Might not be your issue though. – Kasper Mar 10 '17 at 14:22
  • Thank you for the suggestion, however, I tested and that is not the issue. – JP Blonshine Mar 10 '17 at 14:33
  • Not certain, but in a number of places, I think "photo" should be "image". – crmprogdev Mar 10 '17 at 15:17
  • 1
    Take a look at the Base64 String to Image code being used in this dailycoding.com example. Not entirely sure if you're properly converting to an 'Image' from the String in your above code. – TSmith Mar 10 '17 at 15:26
  • This does not seem to be the issue. I am not returning the amount of bytes that would constitute a good photo. – JP Blonshine Mar 10 '17 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.