2

Cognitoでセキュリティ保護されたAPIゲートウェイを設定することができました。認証されていないユーザーの役割には、ゲートウェイにアクセスを許可するアクセスポリシーがあります。また、boto3を使用してプールからアイデンティティIDを取得し、関連付けられたオープンIDトークンと関連付けられたシークレットとアクセスキーを取得しました。PythonでCognito資格情報を使用してAPIゲートウェイを呼び出す方法

これらの資格情報を使用してゲートウェイに電話をかけるにはどうすればよいですか? boto3を使用してAPI上の特定のメソッドへのリクエストの署名を処理する方法はありますか?ここで

答えて

1

は、当社のパブリックのドキュメントからの例です:http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html

Cognitoのcredsを任意の他の一時的なcredsをより違いはありません、と署名プロセスも同じです。上記の例をPythonに戻したいのであれば、あなたのために署名するサードパーティのライブラリがあると思います。

+0

をidentity_pool_id与えます現在、OSX、Android、Javascriptの場合のように、APIゲートウェイ用のPython SDKを生成する機能を追加するには?私はあなたが提供したサンプルドキュメントを見たことがありましたが、Javascript SDKが即時の要件を満たしていれば、究極的にはあまりにも冗長になりました。私はPython用の既存のsigv4ライブラリを探します。このセクションに従って、認証ヘッダーを使用してAPIゲートウェイのメソッドを呼び出しますか?http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html#sig-v4 -examples-get-auth-header – LaserJesus

+0

これはバックログにあります。結局、AWSがサービス用に公開しているのと同じSDKをサポートしたいと考えていますが、ETAはありません。独自のものを使うという点では、既存のPython SDKから署名ライブラリを持ち上げて、サービス名を 'execute-api'に変更するだけです。 –

+0

@LaserJesus:私はあなたの答えを取って、数ヶ月後にAPIジャックは作業中だったと言いましたか?もしそうなら、私はそれを受け入れられた答えとして投票するでしょう。 –

1

次のコード(および要求-aws4authライブラリー)は、仕事をした:

import boto3 
import datetime 
import json 
from requests_aws4auth import AWS4Auth 
import requests 

boto3.setup_default_session(region_name='us-east-1') 
identity = boto3.client('cognito-identity', region_name='us-east-1') 

account_id='XXXXXXXXXXXXXXX' 
identity_pool_id='us-east-1:YYY-YYYY-YYY-YY' 
api_prefix='ZZZZZZZZZ' 

response = identity.get_id(AccountId=account_id, IdentityPoolId=identity_pool_id) 
identity_id = response['IdentityId'] 
print ("Identity ID: %s"%identity_id) 

resp = identity.get_credentials_for_identity(IdentityId=identity_id) 
secretKey = resp['Credentials']['SecretKey'] 
accessKey = resp['Credentials']['AccessKeyId'] 
sessionToken = resp['Credentials']['SessionToken'] 
expiration = resp['Credentials']['Expiration'] 

print ("\nSecret Key: %s"%(secretKey)) 
print ("\nAccess Key %s"%(accessKey)) 
print ("\nSession Token: %s"%(sessionToken)) 
print ("\nExpiration: %s"%(expiration)) 

method = 'GET' 
headers = {} 
body = '' 
service = 'execute-api' 
url = 'https://%s.execute-api.us-east-1.amazonaws.com/dev/helloworld' % api_prefix 
region = 'us-east-1' 

auth = AWS4Auth(accessKey, secretKey, region, service, session_token=sessionToken) 
response = requests.request(method, url, auth=auth, data=body, headers=headers) 
print(response.text) 
+0

api_prefixはどこから来たのですか?ありがとう! – Efren

+1

@Efren APIゲートウェイアプリケーションの管理セクション( .execute-api.us-east-1.amazonaws.com)にアクセスすると、AWSコンソールで見つけることができます。管理領域にある場合、左側のリソースセクションをクリックし、ゲートウェイのエンドポイントの1つ、すなわち/ petsをクリックすると、プレフィックスを含む完全なURLが右側に表示されます – LaserJesus

+0

他のものを取得する場所を記述できますか値もありますか?私はAIMを私より長く使ってきた人にとっては明らかだと確信していますが、このコードは私のために働いていないので、おそらく間違った場所から 'account_id'と' api_prefix'を取得しています。私の 'identity_pool_id'が正しいかどうかはわかりません。ユーザープール→プールの詳細→プールIDに移動すると、私の例ではコロンではなく、下線が表示されます(http://boto3.readthedocs.io/en/latest/reference/のドキュメントのように)。 services/cognito-identity.html#CognitoIdentity.Client.get_id) –

2

私のコードは、主に質問者自身の回答に基づいているが、私はそれがより明確にどこのすべての値にするために試してみましたから来る。あなたはあなたの「identity_pool_id」を与えることができない連合のプールを持っている場合は

を取得する方法を

import boto3 
import requests 
from requests_aws4auth import AWS4Auth 
# Use 'pip install boto3 requests requests-aws4auth' to get these 

region_name = 'ap-southeast-2' # or 'us-west-1' or whatever 

# 12 decimal digits from your AWS login page 
account_id = '123456789012' 

# I've only found this in the sample code for other languages, e.g. JavaScript 
# Services→Cognito→Manage Federated Identities→(your-id-pool)→Sample code 
identity_pool_id = 'ap-southeast-2:fedcba98-7654-3210-1234-56789abcdef0' 

# Create a new identity 
boto3.setup_default_session(region_name = region_name) 
identity_client = boto3.client('cognito-identity', region_name=region_name) 
identity_response = identity_client.get_id(AccountId=account_id, 
    IdentityPoolId=identity_pool_id) 

# We normally wouldn't log this, but to illustrate: 
identity_id = identity_response['IdentityId'] 
print ('identity_id:', identity_id) # good idea not to log this 

# Get the identity's credentials 
credentials_response = identity_client.get_credentials_for_identity(IdentityId=identity_id) 
credentials = credentials_response['Credentials'] 
access_key_id = credentials['AccessKeyId'] 
secret_key = credentials['SecretKey'] 
service = 'execute-api' 
session_token = credentials['SessionToken'] 
expiration = credentials['Expiration'] 
# Again, we normally wouldn't log this: 
print ('access_key_id', access_key_id) 
print ('secret_key', secret_key) 
print ('session_token', session_token) 
print ('expiration', expiration) 
# The access_key_id will look something like 'AKIABC123DE456FG7890', similar to 
# Services→IAM→Users→(AWS_USER_NAME)→Security credentials→Access key ID 

# Get the authorisation object 
auth = AWS4Auth(access_key_id, secret_key, region_name, service, 
    session_token=session_token) 
current_app['auth'] = auth 
# Just an illustration again: 
print ('auth: %(service)s(%(date)s) %(region)s:%(access_id)s' % auth.__dict__) 

# We'll use that object to send a request to our app. This app doesn't 
# exist in real life, though, so you'll need to edit the following quite 
# heavily: 

# Services→Cognito→Manage your User Pools→(your-user-pool)→Apps→App name 
app_name = 'my-app-name' 

api_path = 'dev/helloworld' 
method = 'GET' 
headers = {} 
body = '' 
url = 'https://%s.%s.%s.amazonaws.com/%s' % (app_name, service, region_name, 
    api_path) 
response = requests.request(method, url, auth=auth, data=body, headers=headers) 
0

identity_pool_id、以下 実行コードは、作品に特徴があり、あなたが

import boto3 
boto3.setup_default_session(
    aws_access_key_id='AKIAJ7TBC72BPWNEWIDQ', 
    aws_secret_access_key='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', 
    region_name='ap-southeast-2') 

client = boto3.client('cognito-identity') 
response = client.list_identity_pools(MaxResults=3,) 

print("IdentityPoolId-- ", response) 
関連する問題