This example demonstrates a bare-bones Python implementation of the Square Connect OAuth flow. The application links merchants to the OAuth Permissions form and handles the result of the authorization, which is sent to the application's Redirect URL.
For more information, see OAuth Overview, along with the comments included in oauth-flow.py.
python3 -m venv ./venv
. ./venv/bin/activate
Open your terminal at this directory and type:
pip3 install -r requirements.txt
This application requires Python and the Flask web framework.
-
Open the Developer Dashboard.
-
Choose Open on the card for an application.
-
At the top of the page, set the dashboard mode to the environment that you want to work with by choosing Sandbox or Production.
-
Choose OAuth in the left navigation pane. The OAuth page is shown.
-
In the Redirect URL box, enter the URL for the callback you will implement to complete the OAuth flow:
http://127.0.0.1:5000/callbackYou can use HTTP for localhost but an actual web server implementation must use HTTPS.
-
In the Application ID box, copy the application ID.
-
In the Application Secret box, choose Show, and then copy the application secret.
-
Click Save.
-
In your project directory, create a copy of the
.env.examplefile and name it.env -
In the newly created .env file, replace the
your-environmentwith eithersandboxorproduction -
Replace the
your-application-idandyour-application-secretplaceholders with the Sandbox or Production application ID and application secret, respectively.Note that OAuth Sandbox credentials begin with a sandbox prefix and that the base URL for calling Sandbox endpoints is https://connect.squareupsandbox.com. When you implement for production, you need production credentials and use https://connect.squareup.com as the base URL.
WARNING: Never check your credentials/access_token into your version control system. We've added
.envto the.gitignorefile to help prevent uploading confidential information.
To run the example, execute the following from the command line:
FLASK_APP=oauth-flow.py flask run
You can then proceed through the OAuth flow by going to http://127.0.0.1:5000/ in your web browser.
Rate this sample app here!