【问题标题】:Getting image from Firebase and creating stripe.File从 Firebase 获取图像并创建 stripe.File
【发布时间】:2019-10-07 09:28:13
【问题描述】:

我正在尝试使用 stripe.File.create() 方法将文件从 firebase 数据库传输到 Stripe。这是我正在使用的代码:

file_url = storage.child("/path/to/file").get_url(token=None)
response = requests.get(file_url, stream=True)
    img = Image.open(BytesIO(response.content))
    stripe.File.create(
        purpose="identity_document",
        file=img
    )

但是当我运行这段代码时,我得到:

Request req_E7fskNVgpHHlRm: Invalid hash

我相信我从 firebase 获得了正确的图像,因为我可以运行以下行并将图像保存到我的本地驱动器:

img.save("test.jpg")

但 Stripe 似乎不喜欢我提供的文件格式。我相信该文件必须以二进制模式提供,所以也许我只需要编辑img = Image.open(BytesIO(response.content)) 行即可以二进制模式获取文件。

感谢任何反馈。

【问题讨论】:

  • 您在发送文件时是否设置了正确的 MIME 类型?看看stripe.com/docs/file-upload#uploading-a-file
  • 如何指定 MIME 类型?是在文件头吗?我可以另存为.jpg。该文档没有显示如何设置正确的 MIME 类型

标签: python python-requests stripe-payments python-imageio


【解决方案1】:

我想通了。 img = Image.open(BytesIO(response.content)) 创建一个 JpegImageFile 对象,它是一个图像。但是 Stripe 需要一个文件,比如 io.BufferedReader 对象。所以 Stripe 是说你正在提供一个图像,而实际上它只需要一个文件。要解决此问题,只需将 img = 行更改为:

imgFile = BytesIO(response.content)

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2011-06-12
    相关资源
    最近更新 更多