How can I upload a background image for branding with python SKD?

I have try this

img_file = open("img/monimage.jpg", "rb")
            (
                bckgndimage,
                _,
                error,
            ) = await self.okta_client.upload_brand_theme_background_image(
                brandId=self.brand.id,
                themeId=self.theme.id,
                file=img_file
            )

But I got an issue from Okta API
{‘message’: “Okta HTTP 400 E0000001 Api validation failed: file\n’file’ request parameter is required”}

Is like my file was not passed to API.

I have checked my img_file and is not empty.

Best Regards,
Tristan

Can you make sure the image file you’re attaching meets our criteria?

The image must be a PNG, JPG, or GIF file and be less than 2MB in size.

Hi Tristan, welcome!

Cancel this:

Whoops. Python open() returns a file object, which you can then use to read or write the file (read in your case because of “rb”). The SDK call wants the actual file data! So you need to read the binary data and then pass it. That also explains your error, because the file object is evaluating to “file\n” :slight_smile:

This might help: if you look at the SDK source file brand_client.py you can see on line 366 how it’s using file, it’s setting up the file data as multipart/form data.

What I said here earlier in the block above is incorrect, ignore it. In fact, on the surface your code should work. I spent a couple of hours going through the SDK code and how it works with the aiohttp package underneath. If you pass a Python file object aiohttp will stream the file object and insert the data for you as the multipart-form data. Here it is in the simplest form:

( url, response, error ) = await okta_client.upload_brand_theme_background_image('bnd383zyphjrBz8H60x7', 'the383zypiYcgS2730x7', open('./oktaice2.jpg', 'rb'))

Naming the parameters has no detrimental effect. The only way I’ve had it fail with the error you show is if I pass something bad as the file parameter. This will cause that error, note the third parameter is bad:

( url, response, error ) = await okta_client.upload_brand_theme_background_image('bnd383zyphjrBz8H60x7', 'the383zypiYcgS2730x7', 'xyz')

If your file data was bad you’ld get an error about failing to read the data, so that can’t be it. I see that the indentation is off in your code, so I suspect you copied and pasted it together. Is there any chance that img_file variable is just mucked up between the open and the call to upload_brand_theme_background_image?

Joel

Hi @andrea Yes my picture is a jpg format and weight 1.12MB which sound good.

Does it work at all if you hit the Brands API directly to upload the image, or is the Python SDK itself not working correctly?

Hi @jmussman,

You are right when I running code in standalone little srcript this work perfectly.
The code is failling when include in my program.

This answer to my question How to use this API to loading background image in theme.

I still looking how this faill inside my code and this seems dumb.
The img_file variable isn’t change but I’m working with multi threading and wondering if could have an impact on opened file. I still testing this.

Best Regards,
Tristan

Hi @andrea,

The python SDK work perfectly when I just writing a little testing script the issue arose when I embed the few line in my code. I not sure where it come as the behavior is strange maybe from my multithreading stuff.

Thansk a lot,
Tristan