How to upload a file to Linkedin via Python requests?

Viewed 121

I am trying to upload a video via REST API to LinkedIn. I have completed all the steps and everything is working fine except for the video upload API.

Here is the curl command for upload videos, mentioned on the documentation of LinkedIn:

curl -v -H "Content-Type:application/octet-stream" --upload-file docs/short_video.mp4 'https://api.linkedin.com/mediaUpload/C5400AQHpR1ANqMWqNA/uploadedVideo/0?ca=vector_feedshare&cn=uploads_secure&ccn=ambry-video&m=AQLEZ2pjh43pagYYYXRaCyztOykwDzluHkkYTbsMjNUzivrEOeObw9h3&app=1234&sync=1&v=beta&ut=1KeEm4JnMnJpo1'

When I run this curl command, the video uploaded successfully and I got the status AVAILABLE, but the same thing with python post requests is not working.

Here is the python code that I converted from the curl command:

    headers = {
        'Content-Type': 'application/octet-stream'
    }

    filePath = 'docs/short_video.mp4'

    files_data = {
        'upload_file': (open(filePath, 'rb'))
    }

    post_url = 'https://api.linkedin.com/mediaUpload/C5400AQHpR1ANqMWqNA/uploadedVideo/0?ca=vector_feedshare&cn=uploads_secure&ccn=ambry-video&m=AQLEZ2pjh43pagYYYXRaCyztOykwDzluHkkYTbsMjNUzivrEOeObw9h3&app=1234&sync=1&v=beta&ut=1KeEm4JnMnJpo1'

    req = requests.put(post_url, files=files_data, headers=headers)

I am not sure what I am doing wrong in my python code. Is there something that I am missing? Any kind of help would be greatly appreciated.

1 Answers
upload_request_data = response_data['value']['uploadMechanism'][
            'com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest'
        ]
response = requests.post(
    upload_request_data['uploadUrl'],
    headers=upload_request_data['headers'],
    data=open(filePath, 'rb'),
)
Related