Save image from Grafana using python

I am trying to save image from grafana dashboard using python.

I have created API key and copied link from direct link rendered image but some how it doesn’t working. I have googled to try to solve the issue but I am kind of stuck.

I have tried few different ways.

First

import urllib.request
url = 'http://<IP>:<PORT>/render/.....'
urllib.request.urlretrieve(url, 'test.png')

this worked but the saved image was invalid(nothing showed when I open the image).

Next I have created the API key from the Grafana and used it.

import imgkit
imgkit.from_url(curl "http://<IP>:<port>/render....'-H 'Authorization:Bearer <API Key>' >'test.png')

and above code gave me syntaxError:invalid syntax

I am not sure how to use mixture of API key and the url. so can some one please give me ideas on how to export the image?
If there is alternative way to save image from panel, please let me know

thanks

@eiran

I have read your post but failed to export the image from Grafana.
Do you have ideas what I have missed out?

Thanks for your help in advance.