python调用rest api_调用rest api使用python将数据推送到运动学

我是python和aws的新手.我不知道如何在stackoverflow中提出问题.

请不要阻止我.

我正在尝试发送HTTP发布请求以将记录放入Amazon Kinesis Stream.

我在运动学中创建了一个流mystream.我使用方法发布.

我尝试了以下链接来设置网关api,它工作正常.

我正在尝试使用请求的python代码来做到这一点.

但是我收到以下提到的错误:

以下是我的代码:

import sys, os, base64, datetime, hashlib, hmac

import requests # pip install requests

# ************* REQUEST VALUES *************

method = 'POST'

service = 'kinesis'

host = 'kinesis.eu-west-1.amazonaws.com'

region = 'eu-west-1'

endpoint = 'https://kinesis.eu-west-1.amazonaws.com'

content_type = 'application/x-amz-json-1.1'

amz_target = 'Kinesis_20181114.PutRecord'

request_parameters = '{'

request_parameters += '"StreamName": mystream,'

request_parameters += '"Data": + base64.b64encode(test) + ,'

request_parameters += '"PartitionKey": 1234 '

request_parameters += '}'

# Key derivation functions. See:

# http://docs.aws.amazon.com/general/latest/gr/signature-v4-

examples.html#signature-v4-examples-python

def sign(key, msg):

return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()

def getSignatureKey(key,datestamp,region,service):

kDate = sign(('AWS4' +key ).encode('utf-8'), datestamp)

kRegion = sign(kDate,region)

kService = sign(kRegion,service)

kSigning = sign(kService, 'aws4_request')

return kSigning

# Read AWS access key from env. variables or configuration file. Best

practice is NOT

# to embed credentials in code.

with open ('C:\Users\Connectm\Desktop\acesskeyid.txt') as f:

contents = f.read().split('

')

access_key = contents[0]

secret_key = contents[1]

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()

amzdate = t.strftime('%Y%m%dT%H%M%SZ')

datestamp = t.strftime('%Y%m%d') # Date w/o time, used in credential scope

canonical_uri = '/'

canonical_querystring = ''

canonical_headers = 'content-type:' + content_type + '

' + 'host:' + host +

'

' + 'x-amz-date:' + amzdate + '

' + 'x-amz-target:' + amz_target + '

'

signed_headers = 'content-type;host;x-amz-date;x-amz-target'

payload_hash = hashlib.sha256(request_parameters).hexdigest()

canonical_request = method + '

' + canonical_uri + '

' +

canonical_querystring + '

' + canonical_headers + '

' + signed_headers +

'

' + payload_hash

algorithm = 'AWS4-HMAC-SHA256'

credential_scope = datestamp + '/' + region + '/' + service + '/' +

'aws4_request'

string_to_sign = algorithm + '

' + amzdate + '

' + credential_scope +

'

' + hashlib.sha256(canonical_request).hexdigest()

signing_key = getSignatureKey(secret_key, datestamp, 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

print authorization_header;

headers = {'Content-Type':content_type,

'X-Amz-Date':amzdate,

'X-Amz-Target':amz_target,

'Authorization':authorization_header}

# ************* SEND THE REQUEST *************

print '

BEGIN REQUEST++++++++++++++++++++++++++++++++++++'

print 'Request URL = ' + endpoint

r = requests.post(endpoint, data=request_parameters, headers=headers)

print '

RESPONSE++++++++++++++++++++++++++++++++++++'

print 'Response code: %d

' % r.status_code

print r.text

我收到以下错误

AWS4-HMAC-SHA256凭证= AKIAI5C357A6YSKQFXEA / 20181114 / eu-west-

?1 /运动/ aws4_request,SignedHeaders =内容类型;主机; x-amz日期; x-amz-

?目标,

?签名= 1d7d463e77beaf8693080681212188180db9cc7cff082663ad547f647a9c6d545a

响应

?响应码:400

{“ __type”:“ SerializationException”}

请有人可以解释我如何纠正以上错误吗?

代码是否已连接到流?有关

?数据序列化?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值