人已经吐血,写python七牛云短信接口4小时毫无线索?

人已经吐血,写python七牛云短信接口4小时毫无线索?

python访问七牛云短信接口,接口文档完全看不明白。自己码了半小时完全无进展,快救救孩子吧

import requests

import time

import hmac

import base64

from hashlib import sha1

import json

#def hash_hmac(key, code, sha1):

def hash_hmac(key, code, sha1):

hmac_code = hmac.new(key.encode("utf-8"), code.encode("utf-8"), sha1).digest()

return base64.b64encode(hmac_code).decode()

urlau="/v1/message/fulltext"

meth = "POST"

auData= {

"\nHost:":" sms.qiniuapi.com",

"\nContent-Type:":" application/json"

}

data = {

"mobiles": "13693351446",

"content": "cbjkasc",

"template_type": "notification"

}

list1 =[]

for i in auData.items():

if i[1]!=""and i[0]!="sign":

list1.append("".join(i))

print(list1)

sort ="".join(list1)

austr = str(meth)+"\000"+str(urlau)+"\000"+"HTTP/1.1"+sort

print(austr)

key = "n8oxnmUGu9KpiWxine0_3gE2OySfe4EfXOgCS5sd"

encodedSign = hash_hmac(key,austr,sha1)

print(encodedSign)

url = "https://sms.qiniuapi.com/v1/message/fulltext"

header = {

"Host":"sms.qiniuapi.com",

"Authorization":"Qiniu "+"DaB1wAovX3so7_M7QEGSRL7XDAw4Qn_gbsidyyZ0"+":"+encodedSign,

"Content-Type": "application/json"

}

respon=requests.request('POST',url,data=data,headers=header)

print(respon.text)

`
返回结果如下所示:
Ku+gdPrXHrEtLlAIqBIPb10kQ4A=
['nHost: sms.qiniuapi.com', 'nContent-Type: application/json']
POST /v1/message/fulltext HTTP/1.1
Host: sms.qiniuapi.com
Content-Type: application/json
Vq2TN76f4c2bA6g/HB/ryocgXoI=
{"error":"BadToken","message":"Your authorization token is invalid","request_id":"1N9NB3Q231WkIBQW"}
文档链接如下所示:
https://developer.qiniu.com/s...


回答:

七牛python sdk里,有短信相关的demo
https://github.com/qiniu/pyth...
pip install qiniu可以直接安装


回答:

报错信息明显给出,你的授权token无效。若确定获取和token无误,请仔细检查headers头中Authorization拼接信息。通常这一块,会有不同的认证方式前缀,如 Basic 或Bearer 或其它。像下面这样
Authorization: Bearer JWT\_TOKEN_STR

以上是 人已经吐血,写python七牛云短信接口4小时毫无线索? 的全部内容, 来源链接: utcz.com/a/156697.html

回到顶部