From 87436995fcf543cd91f1be69531ee25105eaa569 Mon Sep 17 00:00:00 2001 From: Harmon Date: Thu, 14 Jan 2021 05:13:25 -0600 Subject: [PATCH] Improve API.update_profile_image file uploading Use files parameter of requests Session.request Allow Twitter's API to respond with "Image validation failed with errors: Image size must be <= 2.0 MiB." and "media type unrecognized." errors instead of checking for file size and type prior to upload. --- tweepy/api.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tweepy/api.py b/tweepy/api.py index cd5f31e..f83fa4f 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -802,7 +802,10 @@ class API: """ :reference: https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_image :allowed_param: 'include_entities', 'skip_status' """ - headers, post_data = API._pack_image(filename, 700, f=file) + if file is not None: + files = {'image': (filename, file)} + else: + files = {'image': open(filename, 'rb')} return bind_api( api=self, path='/account/update_profile_image.json', @@ -810,7 +813,7 @@ class API: payload_type='user', allowed_param=['include_entities', 'skip_status'], require_auth=True - )(self, post_data=post_data, headers=headers) + )(self, files=files) def update_profile_banner(self, filename, **kwargs): """ :reference: https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_banner -- 2.25.1