为什么此代码在我的桌面上的Python中有效,但在Zapier中却无效?

时间:2019-08-20 19:46:02

标签: python zapier

我在Zapier中遇到了一个问题,该代码在我的计算机上的Python中可以正常工作,但始终在zapier中引发错误。我希望这很简单,这样我就可以快速解决此问题。

import requests
import base64

response = requests.get('https://prstvod.s3.amazonaws.com/PursuitUP/PUR86083A_AWS.VTT')
response.raise_for_status() # optional but good practice in case the call fails!
output = response.text
#print (output)
output =  base64.b64encode(output)

#print (output)

url = "https://api.zype.com/videos/5d5c1b165577de355513e24e/subtitles?api_key=NsaCqjERym-vYTa6OiF97Bk9A1BzM6DHYa3SFS8TXPSR6Q78ChQZskFA0RZ2ZT7C"
headers = {'content-type': 'application/json'}
payload = '{"subtitle":{"language":"English","extension_type":"vtt","file":"'+output+'"}}'
response = requests.request("POST", url, data=payload, headers=headers)

output = response.text

print (output)

1 个答案:

答案 0 :(得分:0)

我在这里看到了几件事。

特定于Python 我很惊讶这没有在您的计算机上引发错误,但是b64encode接受了一个字节对象。 response.text函数返回一个解码字符串对象。

Zapier特定 在Zapier中,output必须设置为字典。 output = response.text行将其设置为字符串。

尝试以下代码:

import requests
import base64

response = requests.get('https://prstvod.s3.amazonaws.com/PursuitUP/PUR86083A_AWS.VTT')
response.raise_for_status() # optional but good practice in case the call fails!
output = response.content
#print (output)
output =  base64.b64encode(output)

#print (output)

url = "https://api.zype.com/videos/5d5c1b165577de355513e24e/subtitles?api_key=NsaCqjERym-vYTa6OiF97Bk9A1BzM6DHYa3SFS8TXPSR6Q78ChQZskFA0RZ2ZT7C"
headers = {'content-type': 'application/json'}
payload = '{"subtitle":{"language":"English","extension_type":"vtt","file":"'+str(output)+'"}}'
response = requests.request("POST", url, data=payload, headers=headers)

output = {'response': response.text}

print (output)

提示:您可以在请求调用中使用json参数,而无需手动构建自己的参数。看起来像:

payload = {"subtitle":{"language":"English","extension_type":"vtt","file":str(output)}}
response = requests.request("POST", url, json=payload, headers=headers)