使用linkedin create share api上传二进制图像文件使用python代码给出空响应,但curl命令有效

2024-09-23 22:30:51 发布

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

下面的Curl命令可以很好地工作,但是将Curl命令转换成python代码后就不行了回复.text什么也没印。你知道吗

仅供参考:我正在跟踪this documentation。你知道吗

以下是工作Curl命令:

curl -i --upload-file "/home/abhay/download.jpeg" --header "Authorization: Bearer xxxxxxxxx" 'https://api.linkedin.com/mediaUpload/C5122AQE4GSagETyxQA/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQK_YGFfk_crEwAAAWoGPkMNETNZnhoeCKKmD_CKYeasF1NHooQ7pdeZWA&app=5969925&sync=0&v=beta&ut=1DL2-qd0JrroI1'

转换的python代码

upload_url = 'https://api.linkedin.com/mediaUpload/C5122AQE4GSagETyxQA/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQK_YGFfk_crEwAAAWoGPkMNETNZnhoeCKKmD_CKYeasF1NHooQ7pdeZWA&app=5969925&sync=0&v=beta&ut=1DL2-qd0JrroI1'
headers = {
    'accept' : 'application/json',
    'X-Restli-Protocol-Version': '2.0.0',
    'Authorization': 'Bearer xxxxxxxxx',
    # 'Content-Type': 'multipart/form-data'
    'Content-Type': 'image/jpeg',
}

image = open("/home/abhay/download.jpeg" ,"rb")
files = {'images_file': image }

response = requests.post(upload_url, headers=headers, files=files)
print response.status_code
print response.text

Curl给出了正确的回答:

HTTP/2 201 
date: Wed Apr 10 07:59:03 UTC 2019
server: Play
set-cookie: lang=v=2&lang=en-us; Path=/; Domain=api.linkedin.com
x-ambry-creation-time: Wed Apr 10 07:59:03 UTC 2019
access-control-allow-origin: https://www.linkedin.com
content-length: 0
x-li-fabric: prod-lsg1
x-li-pop: prod-tmu1
x-li-proto: http/2
x-li-uuid: FPDYGfsNlBXg+OFSZisAAA==
set-cookie: lidc="b=SB83:g=115:u=5:i=1554883143:t=1554953528:s=AQEVXPybFdnyKyPqSWbf_ax997ap-22S"
x-li-route-key: "b=SB83:g=115:u=5:i=1554883143:t=1554953528:s=AQEVXPybFdnyKyPqSWbf_ax997ap-22S"

Python代码给出以下响应

201 (status code)
None (response.text)

有人能帮我找出python代码中遗漏的东西吗?谢谢!你知道吗


Tags: 代码texthttpsimage命令comapiresponse