メニュー

Expand
ページを評価:

Twilio Verify Python Flask Quickstart

With just a few lines of code, your Python application can verify phone numbers and add an additional layer of security with Twilio Verify.

This Python Verify Quickstart will teach you how to do this using our Verify REST API, the Twilio Python helper library, and Python’s Flask microframework to ease development.

このクイックスタートでは、下記のことを学んでいきます:

  1. Twilioにサインアップ
  2. Set up your development environment
  3. Send your first SMS phone verification
  4. Check verification codes

Short on time? Spin up a low-code, fully editable verification demo in less than 2 minutes using Twilio's Code Exchange and Quick Deploy here.

Twilioにサインアップ

If you already have a Twilio account, you’re all set here! Feel free to jump to the next step.

Before you can send an SMS from Python, you'll need to sign up for a Twilio account or sign into your existing account.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.

Do I need a phone number?

If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.

Create a Verify Service

Verify uses Services for configuration. To send a Verify API request you will need both your Twilio Credentials and a Service SID. You can create and update a Service in two ways:

  1. In the Verify Console
  2. With the Verify API

Services can be used to edit the name (which shows up in the message template), set the code length (4-10 characters), enable settings like the "do not share warning" and more.

Now that you have a Twilio account and a verification service, you can start writing some code! To make things even easier, we'll next install Twilio's official helper for Python applications.

PythonとTwilioヘルパーライブラリーをインストールする

If you’ve gone through one of our other Python Quickstarts already and have Python and the Twilio Python helper library installed, you can skip this step and get straight to sending your first verification.

To start a phone verification, you’ll need to have Python and the Twilio Python helper library installed.

Pythonをインストールする

あなたがMacやLinuxマシン上にある場合、あなたはおそらくすでにPythonがインストールされています。 ターミナルを開いて下記のコマンドを入力することでこれを確認できます:

python --version

下記のように表示されます:

$ python --version
Python 3.4  # Python 2.7+ is okay too

Windows users can follow this excellent tutorial for installing Python on Windows, or follow the instructions from Python's documentation.

TwilioのPython SDKではPython 2およびPython 3の双方に対応しています。 このクイックスタートはいずれのバージョンでも進められますが、Python 2のみと互換性のある特定のライブラリーがプロジェクトで必要な場合を除き、Twilioを使用する今後のプロジェクトにはPython 3のご使用を推奨します。

        
        
        
        Sends a one-time passcode to a user's phone number

        Send an SMS verification code

        Sends a one-time passcode to a user's phone number
              
              
              
              Checks the one-time passcode sent to the user. The provided code is correct if the response 'status' parameter is 'approved'.

              Check a verification code

              Checks the one-time passcode sent to the user. The provided code is correct if the response 'status' parameter is 'approved'.

              検証アプリケーションをクローンおよびセットアップする

              Start by cloning our Flask repository.

              git clone git@github.com:TwilioDevEd/verify-v2-quickstart-python.git

              If you don't have git installed or prefer to download the source code you can grab a zip file of the project here.

              Set up your virtual environment and install dependencies

              If you're not familiar with Python virtual environments, follow our tutorial for setting up your local Python environment. Navigate into the project folder and create your virtual environment.

              cd verify-v2-quickstart-python
              
              virtualenv venv
              source venv/bin/activate
              
              pip install -r requirements.txt

              Copy .env.example to .env. This is where we'll store sensitive data in environment variables.

              cp .env.example .env

              Run the application

              export FLASK_APP=verify
              export FLASK_ENV=development
              flask init-db
              flask run

              Or on Windows cmd:

              set FLASK_APP=verify
              set FLASK_ENV=development
              flask init-db
              flask run

              If your credentials are set up correctly you'll soon get a message that the app is up!

              Use the Flask Twilio Verify Demo

              Navigate to http://localhost:5000/auth/register. You should see a registration form that looks like this:

              sign up form with phone verification
              Enter your phone number and choose which channel to request verification over. Finally hit the green Sign Up button and wait. You'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (the call will tell you to enter a number on the phone keypad).

              Enter the token into the Verification entry form and click 'Verify':

              verification entry form

              これで、デモアプリケーションはTwilioの電話番号検証で保護されました!

              関連トピック

              Your demo app is now keeping fraudulent users from registering with your business and polluting your database. Next, check out all of the variables and options available to you in the Verify API Reference.

              After that, check out adding additional verification channels supported by the Verify API like:

              Lastly, to protect your service against fraud, view our guidance on Preventing Toll Fraud when using Verify.

              Kelley Robinson Kristian Tran
              ページを評価:

              ヘルプが必要ですか?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

                    
                    
                    

                    フィードバックくださりありがとうございます!

                    We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    Refer us and get $10 in 3 simple steps!

                    ステップ1

                    Get link

                    Get a free personal referral link here

                    ステップ2:

                    Give $10

                    Your user signs up and upgrade using link

                    ステップ3

                    Get $10

                    1,250 free SMSes
                    OR 1,000 free voice mins
                    OR 12,000 chats
                    OR more