This walkthrough provides quick steps to generate a test OAuth token. A best practice is to use a web server to receive the seller authorization. To keep the walkthrough as simple as possible, the redirect URL indicates a callback that is not implemented. You simply let the callback page not be found and use the returned URL that contains the code value you need. For a deeper discussion of the OAuth process, see:
- Create the Redirect URL and Square Authorization Page URL
- Receive Seller Authorization and Manage Seller OAuth Tokens
- Refresh, Revoke, and Limit the Scope of OAuth Tokens
- OAuth API Best Practices
To create a Sandbox OAuth access token:
Sign in to the Developer Dashboard using your developer account.
On the Applications page, scroll down to the Sandbox Test Accounts section and choose Add.
In the Account Name box, enter Seller Test Account.
In the Country box, choose any country.
Clear the Automatically create authorizations for all my current apps checkbox.
Choose Create.
Open your application and choose OAuth in the left pane. Set the Sandbox Redirect URL to http://localhost:8080/callback.
Copy the Sandbox Application ID and Sandbox Application secret values; you use these values in the following steps.
Choose Save.
On the Applications page, go to the Sandbox Test Accounts section and choose Open for the Seller Test Account. The Seller Dashboard opens in a new window.
Open a new tab in the same browser and do the following:
Paste the following URL in the URL address. Replace sandbox-application-ID in the URL with the Sandbox application ID you copied in step 3 and then press Enter.
https://connect.squareupsandbox.com/oauth2/authorize?client_id=<sandbox-application-ID>&scope=PAYMENTS_WRITE%20PAYMENTS_READ%20PAYMENTS_WRITE_SHARED_ONFILE%20CUSTOMERS_WRITEWhen a window opens, choose Allow to grant the developer account the requested permissions.
Square returns a URL with the code you used to get an access token. Because you are not using a web server page to retrieve the URL, the page shows This site can’t be reached. The response URL in the URL address box contains the code you need.
http://localhost:8080/callback?code=sandbox-sq0cgb-fyV9zKASl4npN11LBW9Dmg&response_type=code#_=_Copy the code value. You use this value to obtain an access token with the requested permissions using the
ObtainToken
command. In the following example, replace ```,<application-id>
, and<application-secret>
with the code, Sandbox application ID, and Sandbox application secret, respectively. Run the followingObtainToken
command to obtain an OAuth access token:curl https://connect.squareupsandbox.com/oauth2/token \ -X POST \ -H 'Square-Version: 2022-03-16' \ -H 'Content-Type: application/json' \ -d '{ "grant_type": "authorization_code", "code": "`", "client_id": "<application-id>", "client_secret": "<application-secret>" }' | jqThe following is an example of the JSON response:
{ "access_token": "EAAAEJ0z0nN9cRt3HEPByaX-jT6mPdZFEFerycDL46eHla2lE-F3TexWQH1EG1_", "token_type": "bearer", "expires_at": "2022-06-14T19:30:31Z", "merchant_id": "MLS1XCNYHK3QQ", "refresh_token": "EQAAEBocG3WhN6Xn3ntcfqAMHeJMDUA4HBYLPSt7QXwfXLn7T7K3VoEVlFhLuhaO", "short_lived": false }You can now use the access token to perform permitted actions on behalf of the seller.
Important
This walkthrough is an example of how you obtain an OAuth access token using a Sandbox test account. It is similar to the process you use in production. If you want to have Sandbox test accounts authorized without having to use this process, leave the Automatically create authorizations for all my current apps checkbox selected when you create a new Sandbox test account.
- Connect your Application to a Seller's Account Using OAuth
- Create the Redirect URL and Square Authorization Page URL
- Receive Seller Authorization and Manage Seller OAuth Tokens
- Refresh, Revoke, and Limit the Scope of OAuth Tokens
- OAuth Walkthrough: Authorization Using a Test Account
- OAuth API Best Practices
- OAuth Permissions Reference
- Video: OAuth Best Practices