如何通过Cognito身份池获取临时AWS凭证

时间:2020-09-19 06:11:21

标签: python amazon-web-services aws-api-gateway amazon-cognito

我希望通过Cognito身份池获取临时AWS凭证,以通过API网关将数据发送到Kinesis流。 要注意的一件事是,我试图在没有AWS开发工具包的情况下执行此操作,因为最终将以其编写的语言没有兼容的SDK 。我正在用Python测试解决方案,并且由于不断出现400错误而似乎缺少一些东西。对我在这里做错的任何想法吗?

import requests

url = 'https://cognito-identity.us-east-1.amazonaws.com' #cognito regional endpoint

headers = {
    'X-AMZ-TARGET': 'com.amazonaws.cognito.identity.model.AWSCognitoIdentityService.GetCredentialsForIdentity',
    'X-AMZ-DATE': '20151020T232759Z'
}

body = {
    'IdentityId': 'us-east-1:123456789' #identity id
}

response = requests.post(url = url, data = body, headers = headers)
print(response)

1 个答案:

答案 0 :(得分:0)

我能够通过添加内容类型标头,将主体中的单引号转换为双引号以及将主体字典也用引号包裹来解决此问题。

import requests

url = 'https://cognito-identity.us-east-1.amazonaws.com' #cognito regional endpoint

headers = {
    "CONTENT-TYPE": "application/x-amz-json-1.1",
    "X-AMZ-TARGET": "com.amazonaws.cognito.identity.model.AWSCognitoIdentityService.GetCredentialsForIdentity",
    "X-AMZ-DATE": "20151020T232759Z"
}

body = '''{
    "IdentityId": "123456789"
}'''

response = requests.post(url = url, data = body, headers = headers)
print(response.text)