This example demonstrates a bare-bones Java 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 OAuthHandler.java.
If you want to run this sample against the Square Sandbox environment:
-
Set your application dashboard to Sandbox Settings mode before completing the following steps.
-
Add a new Sandbox Test Account:
a. Click New Account on the dashboard home page.
b. Give the account a name and pick a country.
c. Uncheck Automatically create authorizations for all my current apps. -
Click Launch on the new test account to open the sandbox seller dashboard for the account. The OAuth flow will create an authorization for this account.
On your application dashboard, set your application's Redirect URL to
http://localhost:8000/callback.
Note that applications that don't use a localhost URL must use HTTPS. HTTP is
allowed for localhost URLs to simplify the development process.
First you need to set the version of the Square dependency in pom.xml. Find the line that says REPLACE_ME and input the latest square version for java. The latest version number can be found here
Example - Setting the Square Java SDK version to 40.1.1.20240717
<version>REPLACE_ME</version>
would be
<version>40.1.1.20240717</version>
In order for the sample to work, you must specify the following fields in OAuthHandler.java:
-
Set the value of
ENVIRONMENTto one ofEnvironment.SANDBOX,Environment.PRODUCTIONorEnvironment.CUSTOM -
For sandbox testing, set the value of
CONNECT_HOSTtohttps://connect.squareupsandbox.com. Otherwise, usehttps://connect.squareup.com -
Replace the value of
APPLICATION_IDwith your application's ID, available on your application dashboard. -
Replace the value of
APPLICATION_SECRETwith the application secret, available from the OAuth tab in the Developer Dashboard -
(OPTIONAL) Change the values in the list
SCOPESto the permission set you want to authorize the account to get authorization for. If you do not set this value,MERCHANT_PROFILE_READ PAYMENTS_READ SETTLEMENTS_READ BANK_ACCOUNTS_READare applied.
From this directory, run mvn install to compile the application.
If you are testing in the sandbox, be sure that your test account seller dashboard is still open in another browser tab.
To run the example, execute the following from this directory:
java -cp target/shaded-oauthexample-1.5-SNAPSHOT.jar com.squareup.oauthexample.OAuthHandler
You can then proceed through the OAuth flow by going to http://localhost:8000
in your web browser.
Rate this sample app here!