我正在尝试使用Python向LinkedIn发布视频。
我可以使用curl命令发布视频。
在第二步curl命令:
curl -v
-H "Content-Type:application/octet-stream"
--upload-file ~/Downloads/sample.mp4
"https://www.linkedin.com/dms-uploads/C5505AQH-oV1qvnFtKA/uploadedVideo?sau=aHR0cHM6Ly93d3cubGlua2VkaW4tZWkuY29tL2FtYnJ5L2FtYnJ5LXZpZGVvZWkvP3gtbGktYW1icnktZXA9QVFHVkdRS0FtS05oM2dBQUFYd19ObG1uZzVYcllXajEzZjIybXh4LW55SGVBclVKcE8y"
,我们得到一个响应:
HTTP/1.1 200 OK
Content-Length: 0
Connection: keep-alive
etag: /ambry-videoei/signedId/AQHX97-zKFZrewAAAXx7Kf1h4pHEr_EVJ2_BGyigpJdmMfO9DreqqynKhTXcMMtKmFtM_JL07JFHXfzHoegWPUsBw9ofV7Z3KWfKRvB41FtIXq9pmvQtkDNrw_nPX4QjsDktxxzFq2gMlC9e3H4x8U_y0QQFQO0pMvtdwaBoFZ_eq_hGZRQCG-zTcYD0J_Nqsf1bApcZ_KGM3Zdc62eATjUTAV7zH4f--18-PLbhQMkKl4g9tjzgRVVv3jp3EPwRidBb7bk0RSI.bin
转换为Python时为:
import requests
headers = {
'Content-Type': 'application/octet-stream',
}
params = {
'sau': 'aHR0cHM6Ly93d3cubGlua2VkaW4tZWkuY29tL2FtYnJ5L2FtYnJ5LXZpZGVvZWkvP3gtbGktYW1icnktZXA9QVFHVkdRS0FtS05oM2dBQUFYd19ObG1uZzVYcllXajEzZjIybXh4LW55SGVBclVKcE8y',
}
with open('~/Downloads/sample.mp4', 'rb') as f:
data = f.read()
response = requests.put(
'https://www.linkedin.com/dms-uploads/C5505AQH-oV1qvnFtKA/uploadedVideo',
params=params,
headers=headers,
data=data
)
response
<Response [200]>
我希望response
中的etag
继续进行进一步的步骤。所以我试了:
data = json.loads(response.content)
当我print(response.content)
时,我得到:
b ' '
我也试过
print(response.json())
我得到了这个错误:
File "xxxxx/env/lib/python3.10/site-packages/requests/models.py", line 917, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: [Errno Expecting value] : 0
有没有人可以帮助我从这个代码中获得特定标签的响应?
经过一番深入研究,终于找到了我的问题的答案。我检查了更多关于什么是ETag和它的用途,发现它是header部分的一部分,所以只是尝试
response.headers
并且能够得到ETag
和提取ETag我尝试
etag = respHeaders['ETag']
现在我可以成功上传视频到LinkedIn了。