facebook-python-business-sdk icon indicating copy to clipboard operation
facebook-python-business-sdk copied to clipboard

Impossible to create an AdImage by using the bytes directly

Open m-vdb opened this issue 10 years ago • 4 comments

Hi there,

Because of this mixin's method it's now impossible to call AdImage().remote_create() by specifying bytes directly, whereas the API documentation says so. I understand that this remote_create() method on the mixin is a shorthand for users that want upload images from the disk, but it's a pain for every one else that needs to use the bytes directly.

What I suggest:

  • either rename the method on the mixin to something like remote_create_from_file() which would make way more sense BUT would introduce a breaking change
  • either complexify the method so that it tries to read a file on disk only if no bytes parameter was used

What do you think? @daphyFB

m-vdb avatar Sep 22 '16 15:09 m-vdb

@m-vdb Thank you for reporting the issues. We will consider adding this feature in a future release.

lintaocui avatar Sep 23 '16 23:09 lintaocui

we patched it on our side so it's ok. do you know if it's coming in 2.8 or later? thanks @lintaocui

m-vdb avatar Sep 24 '16 00:09 m-vdb

If anyone else encounters this issue, you can upload bytes directly (in python 3) with something like this:

from io import BytesIO
import PIL

from base64 import b64encode
from facebookads import FacebookAdsApi, FacebookSession

if __name__ == '__main__':
    image = PIL.Image.open('image.jpg')
    image_string_io = BytesIO()
    image.save(image_string_io, format=image.format)
    image_bytes = image_string_io.getvalue()

    session = FacebookSession(
        FACEBOOK_APP_ID,
        FACEBOOK_APP_SECRET,
        FACEBOOK_FB_ACCESS_TOKEN,
    )
    api_instance = FacebookAdsApi(session)

    response = api_instance.call(
        'POST', ['act_{0}'.format(FACEBOOK_AD_ACCOUNT), 'adimages'],
        params={'bytes': b64encode(image_bytes).decode('utf-8')}
    )

    image_hash = response.json()['images']['bytes']['hash']

cscanlin avatar Nov 24 '16 11:11 cscanlin

It seems like this issue has been open for more than a few months now. We have made many changes in the SDK since then, including releasing a new version. If you can repro your specific issue with the latest version of the SDK, please create a new issue or comment here with further details.

codytwinton avatar Aug 27 '18 18:08 codytwinton