Linkedin Api 图片上传 return status_code 400 使用 python 但使用 curl 工作正常

Linkedin Api Image Upload return status_code 400 using python but works fine using curl

curl 命令完美运行,但 returns 使用 python

时状态代码为 400
curl -i --upload-file "/Users/peter/Desktop/superneatimage.png" --header "Authorization: xxxxxx redacted" 'https://api.linkedin.com/mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtBiZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-IhpbfXrRow1'

python

URL = "https://api.linkedin.com/mediaUpload/C5622AQEgpDjq-5lt3w/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJcz8wFt-0gZQAAAXJ_zhVP13hra1LIIu-fTX8QY9G4z6JcqRd6PXv_vw&app=68078486&sync=0&v=beta&ut=0zcFClAHwoHVg1
"
image = open("/Users/peter/Desktop/superneatimage.png", "rb")

file = {
    "upload-file": image
}

headers = {
            "X-Restli-Protocol-Version": "2.0.0",
            "Authorization": f"Bearer {accessToken}"
        }

action = requests.post(url, files=file, headers=headers)
print(action.status_code)

python输出

400

使用文件读取并将读取的图像传递给请求方法中的数据参数。参考下面的代码

URL = "https://api.linkedin.com/mediaUpload/C5622AQEgpDjq-5lt3w/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJcz8wFt-0gZQAAAXJ_zhVP13hra1LIIu-fTX8QY9G4z6JcqRd6PXv_vw&app=68078486&sync=0&v=beta&ut=0zcFClAHwoHVg1"
image = open("/Users/peter/Desktop/superneatimage.png", "rb").read()

headers = {
   "Authorization": f"Bearer {accessToken}"
}

action = requests.put(url, data=image, headers=headers)
print(action.status_code)