API Uploading User Photos

Kyle.L.2357
Kyle.L.2357 Posts: 8 🌱

I am trying to upload users Photos using the API.
I receive a successful response from each post; however the images do not change.

I will post a screenshot of my code in C# below and would greatly appreciate it if someone can see anything wrong with the code.

image.png
Tagged:

Comments

  • Derik.P.149
    Derik.P.149 Posts: 23 Analytics Builder Transition

    I had a problem where Postman was turning my POST requests into GET requests when the API had a redirect and I had to change that behaviour in a setting. This may be the case in your C# library as well.

    I would recommend checking what you get in the body/headers of the response and see if it matches the GET request for the endpoint. It might also be worth checking if there's a way to disable automatic redirects in your C# library.

  • Kyle.L.2357
    Kyle.L.2357 Posts: 8 🌱

    Thanks! I will give that a try

  • Alison.F.745
    Alison.F.745 Posts: 6 Analytics Builder Transition

    We are bulk updating profile photos successfully for our students through python (requests library). One thing that stands out to me in your code as a possible issue is this line:

    form.Add(content, "profileImage", fileName); This "fileName" might have to be the full path to the file instead of just the name, so "~/Photos/" + fileName.

    I can share our python code with you if that is helpful.

  • Kyle.L.2357
    Kyle.L.2357 Posts: 8 🌱

    Thanks so much.
    Yes, if you don't mind sharing, I would love to take a look at it.

    I'm not apposed to going the Python route.

  • Kyle.L.2357
    Kyle.L.2357 Posts: 8 🌱

    Alison.F.745, I gave your suggestion a try with no luck unfortunately. I thought you had something there.

    The funny thing is: I received a successful response back with the whole path and with just the filename.
    So I attempted just to put a "test" string in to see what would happen.\

    I again got a successful response.
    I thought that's crazy, I only passed in two arguments to see what would happen and i finally got a bad request.

  • Kyle.L.2357
    Kyle.L.2357 Posts: 8 🌱

    I'm trying to look through the documentation to see what they are expecting to be posted.

  • Alison.F.745
    Alison.F.745 Posts: 6 Analytics Builder Transition

    @Kyle.L.2357

    I apologize for the delay, I didn't get a notification. I'll post my python code below, it's edited a little because we have some of these values set as variables in our config file.

    I looked again at our code, this was written a few years ago at this point, and our "filename" variable is just the filename and extension just like your code and "filepath" is the full path. So that's definitely not your issue. I am wondering if maybe it's a scope and/or permission issue? I'm using 'users:profile:read users:userdata:read' for the scope. I will try to get a screenshot of our permissions for this user account we use for this process. I do remember we needed to enable some search permission to be able to set the profile photo, which doesn't make sense.

    def photo_update_request(access_token, api_service_info, filename, filepath, brightspace_user_id, org_defined_id):
    user_profile_image_url = "https://brightspace.marist.edu/d2l/api/lp/1.35/profile/user/{0}/image"

    logger = logging.getLogger(__name__)

    with open(filepath, 'rb') as file_obj:
    files = [
    ('profileImage', (filename, file_obj, 'image/jpeg'))
    ]

    current_user_profile_image_url = user_profile_image_url.format(brightspace_user_id)
    response = requests.post(current_user_profile_image_url, files=files, auth=BearerTokenAuth(access_token))

    if response.status_code != 200:
    log_str_map = f"Unable to update user's profile image, skipping. (Status code: {response.status_code})"
    log_str = f"{org_defined_id}: Unable to update user's profile image, skipping. (Status code: {response.status_code})"
    logger.info(log_str)
    return False, log_str_map

    log_str_map = "Successfully updated user's profile image"
    log_str = f"{org_defined_id}: Successfully updated user's profile image"
    logger.info(log_str)

    return True, log_str_map
  • Alison.F.745
    Alison.F.745 Posts: 6 Analytics Builder Transition
    image.png

    This is the for a custom role we have just for the user that uploads the profile photos.

  • Alison.F.745
    Alison.F.745 Posts: 6 Analytics Builder Transition
    image.png

    These are all under the "Users" tool when editing permissions.