我正在尝试编写一个shell脚本,使用cURL将base64
中编码的文件上传到cloudinary
#!/bin/bash
timestamp=$(date +%s)
apiSecret=$2
fileName=$1
data="api_key=679764637516936&file=$(base64 -w 0 $fileName)×tamp=${timestamp}"
datatobehashed="timestamp=${timestamp}$apiSecret"
hash=$(echo ${datatobehashed} | sha1sum | awk '{print $1}')
curl -v "https://api.cloudinary.com/v1_1/zolatech/raw/upload --data \"${data}&signature=${hash}\""
echo ""
但它会返回400 Bad Request
,并返回以下回复{"error":{"message":"Invalid Signature 55683272b2d893c0d140af596a01d23977ede889. String to sign - 'timestamp=1484757367'."}}
所以我没有正确使用API吗?或我的代码有问题吗?
答案 0 :(得分:1)
哈希未正确回显,导致预期sha1
不匹配:
hash=$(echo -n ${datatobehashed} | sha1sum | awk '{print $1}')
你应该使用echo -n
,否则你最终会得到一个尾随换行符和坏哈希。
回声 -n
-n不要打印尾随换行符。