TencentCloud / tencentcloud-sdk-python

Tencent Cloud API 3.0 SDK for Python
Apache License 2.0
613 stars 251 forks source link

ASR api: AuthFailure.SignatureFailure #16

Closed yunhenk closed 6 years ago

yunhenk commented 6 years ago

[TencentCloudSDKException] code:AuthFailure.SignatureFailure message:The provided credentials could not be validated. Please check your signature is correct

i called the SpeechRecognition API, don't know why cause the upper exception, here is my code

def tobase64(file_path):
    f=open(file_path,'rb')
    res=base64.encodebytes(f.read())
    f.close()
    return res
try:

    config=configparser.ConfigParser()
    config.read('.config')
    secret_id=config.get('tencent_cloud_1','SecretId')
    secret_key=config.get('tencent_cloud_1','SecretKey')
    print(secret_id)
    print(secret_key)

    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(secret_id, secret_key)

    clientProfile = ClientProfile()
    clientProfile.signMethod = "HmacSHA256"

    client=aai_client.AaiClient(cred,"ap-beijing",clientProfile)
    req=models.SentenceRecognitionRequest()

    req.ProjectId=0
    req.SubServiceType=2
    req.EngSerViceType = '16k'
    req.SourceType = 1
    req.VoiceFormat = 'mp3'
    req.UsrAudioKey = '123'

    req.Data = tobase64('fengtian.mp3') # base64 format, <900k
    req.DataLen = len(req.Data)

    resp=client.SentenceRecognition(request=req)
    print(resp.to_json_string())
except:
   ... 
yunhenk commented 6 years ago

The data for req should be string other than bytes, ie: you need to decode after base64 encode. What's more, the encode method should be b64encode other than encodebytes. The right code looks like:

def tobase64(file_path):
    f=open(file_path,'rb')
    res=base64.b64encode(f.read())
    f.close()
    return res.decode()
zqfan commented 6 years ago

Thanks for your report. Are you running this code in python 3 environment?

yunhenk commented 6 years ago

yes, python 3.5.2