when i'm using python SDK for sent a post request?

  • 回答 (1)
  • 关注 (0)
  • 查看 (26)

I want sent a post request

i have to use English ,becasue my RaspberryPi don't have pinyin

please use Cinese answer the question .

exception:

[TencentCloudSDKException] code:AuthFailure.SignatureFailure message:The provided credentials could not be validated because of exceeding request size limit, please use new signature method `TC3-HMAC-SHA256`. requestId:92c2a31c-4afe-4f34-a9e2-dc4fe2b35fc8

my code:

# helloworld/helloworld/view.py
from PIL import Image
import uuid
from django.http import HttpResponse
import json
from picamera import PiCamera
from time import sleep
import random
import requests
import datetime
import os
import base64
import sys
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException 
from tencentcloud.iai.v20180301 import iai_client, models 

def get_size(file):
    # 获取文件大小:KB
    size = os.path.getsize(file)
    return size / 1024

def get_outfile(infile, outfile):
    if outfile:
        return outfile
    dir, suffix = os.path.splitext(infile)
    outfile = '{}-out{}'.format(dir, suffix)
    return outfile

def compress_image(infile, outfile='', mb=9000, step=10, quality=80):
    """不改变图片尺寸压缩到指定大小
    :param infile: 压缩源文件
    :param outfile: 压缩文件保存地址
    :param mb: 压缩目标,KB
    :param step: 每次调整的压缩比率
    :param quality: 初始压缩比率
    :return: 压缩文件地址,压缩文件大小
    """
    o_size = get_size(infile)
    if o_size <= mb:
        return infile
    outfile = get_outfile(infile, outfile)
    while o_size > mb:
        im = Image.open(infile)
        im.save(outfile, quality=quality)
        if quality - step < 0:
            break
        quality -= step
        o_size = get_size(outfile)
    return outfile, get_size(outfile)

def createAnImage():
    camera = PiCamera()
    # camera.start_preview()
    camera.rotation = 180
    sleep(2)

    fileNameArry = []
    for i in range(0, 5):
        fileNameArry.append(random.randint(1, 50))
    fileName = "".join(str(word) for word in fileNameArry)
    longFileName = "/home/pi/Desktop/images/" + fileName + ".jpg"
    camera.capture(longFileName)
    compress_image(longFileName)
    # camera.stop_preview()
    return longFileName

def imageToBase64(langFileName):
    
    with open(langFileName, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        s = base64_data.decode()
        return s

def creatPerson(FileName,personName,personId):

    try:
        cred = credential.Credential("AKIDthNtIXWTczdCdxrJM9qLivxnRQRGFLbi", "5zIWZP3WouoPuENKLCrL5g6OVTGswuWL") 
        httpProfile = HttpProfile()
        httpProfile.endpoint = "iai.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = iai_client.IaiClient(cred, "ap-beijing", clientProfile)

        req = models.CreatePersonRequest()
        params = '{"GroupId":"group1","PersonName":"'+ personName +'","PersonId":"'+ personId +'","Image":"'+ imageToBase64(FileName) +'"}'
        req.from_json_string(params)

        resp = client.CreatePerson(req) 
        print(resp.to_json_string()) 

    except TencentCloudSDKException as err: 
        print(err) 


def addFamily(request):
    global return_json

    if request.is_ajax():

        data = json.loads(request.body.decode("utf8"))
        
        print(data)
        
        tencentReturnFlag = creatPerson()

        if BaiduReturnFlag == "0":
            return_json = {"msg": "添加成功"}
        else:
            return_json = {"msg": "添加失败"}
        
        os.remove(longFileName)

        return HttpResponse(json.dumps(return_json))
    
creatPerson(createAnImage(),"didi","201703204322")

FFFF0H.FFFF0H.提问于
流星留步Android, H5, ReactNative, 小程序回答于

你这是签名过期,看起来是签名方法有问题。

可能回答问题的人

  • Y. Ji

    6 粉丝0 提问1 回答
  • Superbeet

    4 粉丝0 提问0 回答
  • 腾讯云AI中心

    腾讯云 · 产品经理 (已认证)

    63 粉丝0 提问19 回答
  • 莫烦

    3 粉丝0 提问0 回答
  • 罗罗可爱多

    腾讯 · 高级研究员 (已认证)

    9 粉丝0 提问4 回答
  • 晏栋栋栋

    2 粉丝0 提问1 回答

扫码关注云+社区

领取腾讯云代金券