OTP Amazon Pinpoint で SDK for Python (Boto3) を使用するためのコード例 - Amazon Pinpoint

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

OTP Amazon Pinpoint で SDK for Python (Boto3) を使用するためのコード例

このセクションでは、 SDK for Python (Boto3) を使用してコードを送信および検証する方法を示すOTPコード例を示します。

リファレンス ID を生成する

次の関数は、受信者の電話番号、受信者が を受け取る製品またはブランドOTP、およびリクエストのソース (サイトやアプリのページ名など) に基づいて、受信者ごとに一意の参照 ID を生成します。OTP コードを検証するときは、検証を成功させるために同じ参照 ID を渡す必要があります。送信コードおよび検証コードの例では、いずれもこのユーティリティ関数を使用しています。

この関数は必須ではありませんが、検証ステップ中に簡単に再送信できるように、特定のトランザクションへのOTP送信と検証プロセスをスコープする便利な方法です。任意の参照 ID を使用できます。これは基本的な例に過ぎません。ただし、このセクションの他のコード例は、この関数に依存しています。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import hashlib def generate_ref_id(destinationNumber,brandName,source): refId = brandName + source + destinationNumber return hashlib.md5(refId.encode()).hexdigest()

送信OTPコード

次のコード例は、 SDK for Python (Boto3) を使用してOTPコードを送信する方法を示しています。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import boto3 from botocore.exceptions import ClientError from generate_ref_id import generate_ref_id ### Some variables that are unlikely to change from request to request. ### # The AWS Region that you want to use to send the message. region = "us-east-1" # The phone number or short code to send the message from. originationNumber = "+18555550142" # The project/application ID to use when you send the message. appId = "7353f53e6885409fa32d07cedexample" # The number of times the user can unsuccessfully enter the OTP code before it becomes invalid. allowedAttempts = 3 # Function that sends the OTP as an SMS message. def send_otp(destinationNumber,codeLength,validityPeriod,brandName,source,language): client = boto3.client('pinpoint',region_name=region) try: response = client.send_otp_message( ApplicationId=appId, SendOTPMessageRequestParameters={ 'Channel': 'SMS', 'BrandName': brandName, 'CodeLength': codeLength, 'ValidityPeriod': validityPeriod, 'AllowedAttempts': allowedAttempts, 'Language': language, 'OriginationIdentity': originationNumber, 'DestinationIdentity': destinationNumber, 'ReferenceId': generate_ref_id(destinationNumber,brandName,source) } ) except ClientError as e: print(e.response) else: print(response) # Send a message to +14255550142 that contains a 6-digit OTP that is valid for 15 minutes. The # message will include the brand name "ExampleCorp", and the request originated from a part of your # site or application called "CreateAccount". The US English message template should be used to # send the message. send_otp("+14255550142",6,15,"ExampleCorp","CreateAccount","en-US")

OTP コードの検証

次のコード例は、 SDK for Python (Boto3) を使用して、既に送信したOTPコードを検証する方法を示しています。認証のステップを成功させるためには、お客様のリクエストに、メッセージの送信に使用された参照 ID と完全に一致する参照 ID が含まれている必要があります。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import boto3 from botocore.exceptions import ClientError from generate_ref_id import generate_ref_id # The AWS Region that you want to use to send the message. region = "us-east-1" # The project/application ID to use when you send the message. appId = "7353f53e6885409fa32d07cedexample" # Function that verifies the OTP code. def verify_otp(destinationNumber,otp,brandName,source): client = boto3.client('pinpoint',region_name=region) try: response = client.verify_otp_message( ApplicationId=appId, VerifyOTPMessageRequestParameters={ 'DestinationIdentity': destinationNumber, 'ReferenceId': generate_ref_id(destinationNumber,brandName,source), 'Otp': otp } ) except ClientError as e: print(e.response) else: print(response) # Verify the OTP 012345, which was sent to +14255550142. The brand name ("ExampleCorp") and the # source name ("CreateAccount") are used to generate the correct reference ID. verify_otp("+14255550142","012345","ExampleCorp","CreateAccount")