Skip to main content
New Participant
Mar 9, 2023 11:26 AM UTC
Question

Update/Upload User Profile Picture

Hi, I want to upload and update a user's profile image with no luck, I tried various approaches non work, even the code in the documentation, the code returns "Parameter [image] value is invalid."

Following is my code.

# ----- views.py ----- #
class UpdateUserProfilePicture(APIView):
    permission_classes = [IsAdminUser]
    authentication_classes = [CustomeAuthentication]
    
    def post(self, request, extensionId):
        data = request.data
        file_data = data.pop('image')
        data["image"] = file_data
        res = updateUserProfilePicture(extensionId, data)
        return Response(res)
# ----- api.py ----- #
def updateUserProfilePicture(extensionId, data):
    platform, rcsdk = login()
    imgType = Image.open(BytesIO(data["image"][0].read())).format.lower()
    image = (data["image"][0].name, data["image"][0].read(), f'image/{imgType}')
    builder = rcsdk.create_multipart_builder()
    builder.add(image)
    try:
        request = builder.request(f"https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/{extensionId}/profile-image", 'PUT')
        res = platform.send_request(request)
        return res
    except Exception as e:
        return ({"error": str(e)})


Note that the login function workes fine, abd both platform, and rcsdk, as I managed to upload a greeting with almost the same approach.

    1 reply

    PhongVu
    Community Manager
    Community Manager
    Mar 9, 2023 7:39 PM UTC

    There was an issue with the old RingCentral Python SDK. We just updated the SDK that fixed the issue.

    Update the RingCentral Python SDK: pip3 install --upgrade ringcentral

    And modify your code as follows:

    def updateUserProfilePicture(extensionId, data):
        platform, rcsdk = login()
        imgType = Image.open(BytesIO(data["image"][0].read())).format.lower()
        image = (data["image"][0].name, data["image"][0].read(), f'image/{imgType}')
        builder = rcsdk.create_multipart_builder()
        builder.add(image, 'image')
        try:
            request = builder.request(f"/restapi/v1.0/account/~/extension/~/profile-image", 'PUT')
            res = platform.send_request(request)
            return res
        except Exception as e:
            return ({"error": str(e)})