I followed the example in https://docs.aws.amazon.com/transcribe/latest/dg/getting-started-http-websocket.html to get a transcription job information and also followed the POST example in https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html for signature signing. However, I am getting 404 <UnknownOperationException/>
Because of legacy code, we are using python 2.7 so I cannot use AWS SDK / boto3.
Here is the debug output:
BEGIN REQUEST++++++++++++++++++++++++++++++++++++
Request URL = https://transcribe.us-west-2.amazonaws.com/
data = {"TranscriptionJobName": "test1"}
headers = {'Authorization': 'AWS4-HMAC-SHA256 Credential=********/20220419/us-west-2/transcribe/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date;x-amz-target;x-amz-security-token, Signature=*******',
'Content-Type': 'application/x-amz-json-1.0',
'X-Amz-Date': '20220419T205700Z',
'X-Amz-Target': 'com.amazonaws.transcribe.Transcribe.GetTranscriptionJob'}
RESPONSE++++++++++++++++++++++++++++++++++++
Response code: 404
<UnknownOperationException/>
And here is the code:
import sys, os, base64, datetime, hashlib, hmac
import requests # pip install requests
import pprint
# ************* REQUEST VALUES *************
method = 'POST'
service = 'transcribe'
host = 'transcribe.us-west-2.amazonaws.com'
region = 'us-west-2'
endpoint = 'https://transcribe.us-west-2.amazonaws.com/'
content_type = 'application/x-amz-json-1.0'
amz_target = 'com.amazonaws.transcribe.Transcribe.GetTranscriptionJob'
# params for GetTranscriptionJob
request_parameters1 = '{'
request_parameters1 += '"TranscriptionJobName": "test1"'
request_parameters1 += '}'
# params for StartTranscriptionJob
request_parameters2 = '{'
request_parameters2 += '"TranscriptionJobName": "test2 via https",'
request_parameters2 += '"IdentifyLanguage": true,'
request_parameters2 += '"Media": { "MediaFileUri": "********mp3"},'
request_parameters2 += '"Subtitles": true'
request_parameters2 += '}'
request_parameters = request_parameters1
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
def getSignatureKey(key, date_stamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), date_stamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
# access_key = os.environ.get('AWS_ACCESS_KEY_ID')
# secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
access_key = '**************'
secret_key = '**************'
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amz_date = t.strftime('%Y%m%dT%H%M%SZ')
date_stamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope
canonical_uri = '/'
canonical_querystring = ''
canonical_headers = 'content-type:' + content_type + '\n' + 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n' + 'x-amz-target:' + amz_target + '\n'
signed_headers = 'content-type;host;x-amz-content-sha256;x-amz-date;x-amz-target;x-amz-security-token'
payload_hash = hashlib.sha256(request_parameters.encode('utf-8')).hexdigest()
canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = date_stamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
signing_key = getSignatureKey(secret_key, date_stamp, region, service)
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature
headers = {'Content-Type':content_type,
'X-Amz-Date':amz_date,
'X-Amz-Target':amz_target,
'Authorization':authorization_header}
# ************* SEND THE REQUEST *************
print('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++')
print('Request URL = ' + endpoint)
print('data = ' + request_parameters)
print('headers = ' + pprint.pformat(headers))
r = requests.post(endpoint, data=request_parameters, headers=headers)
print('\nRESPONSE++++++++++++++++++++++++++++++++++++')
print('Response code: %d\n' % r.status_code)
print(r.text)