【问题标题】:Linkedin Api Image Upload return status_code 400 using python but works fine using curlLinkedin Api Image Upload 使用 python 返回 status_code 400 但使用 curl 可以正常工作
【发布时间】:2020-09-23 14:57:25
【问题描述】:

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'

蟒蛇

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

【问题讨论】:

    标签: python curl python-requests linkedin-api


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2021-10-05
      • 2021-05-20
      相关资源
      最近更新 更多