Linkedin Api图像上传返回状态代码400使用python,但使用curl可以正常工作

2024-09-23 22:24:45 发布

您现在位置:Python中文网/ 问答频道 /正文

curl命令工作正常,但在使用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

Tags: httpscomapipngcurlusersfilepeter
1条回答
网友
1楼 · 发布于 2024-09-23 22:24:45

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

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)

相关问题 更多 >