0

ユーザーをcognitoに追加してグループに割り当てようとしています。グループに追加することは成功ですが、ユーザーが属するグループを追加することはできません。Python SDKを使用してCognitoユーザーのグループを追加する方法

import boto3 

email = '[email protected]' 
poolId = 'ap-northeast-1_xxxxxx' 
group = 'xxxx' 
password = 'String1234' 
aws_access_key_id = 'Axxxxxxxx' 
aws_secret_access_key = '6xxxxxxxxx' 

session = boto3.Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) 
client = session.client('cognito-idp') 

response = client.admin_create_user(
       UserPoolId=poolId, 
       Username=email, 
       TemporaryPassword= password, 
       UserAttributes=[ 

        { 
         "Name": "email", 
         "Value": email 
        }, 
        { 
         "Name": "email_verified", 
         "Value": "true" 
        } 
       ] 
      ) 

response = client.admin_add_user_to_group(
       UserPoolId=poolId, 
       Username=email, 
       GroupName=group 
      ) 

ユーザーが正常に追加されますが、グループが追加されていません。

Traceback (most recent call last): File "test.py", line 24, in <module> 
response = client.admin_add_user_to_group 
(AttributeError: 'CognitoIdentityProvider' object has no attribute 'admin_add_user_to_group' 

を私のrequirements.txtではPython用の仮想enviormentを作成するために、私はthis boto3ドキュメントを使用して

boto3==1.4.5 

を使用します参考のため。

答えて

0

私はboto3(1.4.6)で動作させました。ここにある例は、ユーザーを追加しようとしているグループを作成したことを前提としています。また、次のコードサンプルでは、​​環境変数として秘密鍵へのアクセスをエクスポートしたことを前提としています。

import boto3 
a='XXXXXXXXXXXXXXXXXXXX' 
s='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
r='us-west-2' 

session = boto3.Session(aws_access_key_id=a, aws_secret_access_key=s, region_name=r) 
client = boto3.client('cognito-idp', region_name=r) 

email = '[email protected]' 
poolId = 'us-west-2_xxxxxxxxx' 
group = 'foo' 
password = 'String1234' 

response = client.admin_create_user(
       UserPoolId=poolId, 
       Username=email, 
       TemporaryPassword= password, 
       UserAttributes=[{"Name": "email","Value": email}, { "Name": "email_verified", "Value": "true" }] 
      ) 

reply = client.create_group(UserPoolId=poolId, GroupName=group) 
reply = client.admin_add_user_to_group(UserPoolId=poolId, Username=email, GroupName=group) 
関連する問題