Overview
The OAuth client provides methods for:- Obtaining access tokens using authorization codes or refresh tokens
- Revoking access tokens
- Retrieving token status information
- Authorizing applications
Client Initialization
Methods
ObtainToken
Returns an OAuth access token and refresh token using theauthorization_code or refresh_token grant type.
oauth.go:176
The Square-issued ID of your application, available as the Application ID on the OAuth page in the Developer Console.
The secret key for your application, available as the Application secret on the OAuth page. Required for the code flow for any grant type.
The authorization code to exchange for an OAuth access token. Required for the code flow and PKCE flow if
grant_type is authorization_code.The redirect URL for your application. Required for the code flow and PKCE flow if
grant_type is authorization_code and you provided the redirect_uri parameter in your authorization URL.The method used to obtain an OAuth access token. Valid values:
authorization_code- Requires thecodefieldrefresh_token- Requires therefresh_tokenfieldmigration_token- LEGACY for access tokens obtained using a Square API version prior to 2019-03-13
A valid refresh token for generating a new OAuth access token. Required for the code flow and PKCE flow if
grant_type is refresh_token.The list of permissions that are explicitly requested for the access token. Optional for the code flow and PKCE flow if
grant_type is refresh_token.Indicates whether the returned access token should expire in 24 hours. Optional. The default value is
false.The secret your application generated for the authorization request. Required for the PKCE flow if
grant_type is authorization_code.The OAuth access token for accessing Square APIs.
The type of token returned (typically “bearer”).
The date when the access token expires, in RFC 3339 format.
The refresh token for obtaining new access tokens.
RevokeToken
Revokes an access token generated with the OAuth flow. If an account has more than one OAuth access token for your application, this endpoint revokes all of them.oauth.go:51
The Square-issued ID for your application, available on the OAuth page in the Developer Dashboard.
The access token of the merchant whose token you want to revoke. Do not provide a value for
merchant_id if you provide this parameter.The ID of the merchant whose token you want to revoke. Do not provide a value for
access_token if you provide this parameter.If
true, terminate the given single access token, but do not terminate the entire authorization. Default: falseIndicates whether the revocation was successful.
RetrieveTokenStatus
Returns information about an OAuth access token or an application’s personal access token.oauth.go:390
Add the access token to the Authorization header of the request in the format:
Authorization: Bearer ACCESS_TOKENThe list of scopes associated with the token.
The date when the access token expires, in RFC 3339 format.
The Square application ID associated with the token.
The ID of the merchant associated with the token.
OAuth Flows
Code Flow
The standard OAuth 2.0 authorization code flow:-
Redirect to Square’s authorization page:
- Merchant authorizes your application
- Square redirects back with an authorization code
-
Exchange code for access token:
Refresh Token Flow
Refresh an expired access token:PKCE Flow
Proof Key for Code Exchange (PKCE) for public clients:Best Practices
- Store tokens securely: OAuth tokens should be encrypted and stored on a secure server
- Never expose secrets: Keep your client secret and access tokens out of client-side code
- Use refresh tokens: Implement token refresh to maintain long-term access
- Respect scopes: Only request the minimum scopes needed for your application
- Handle expiration: Check token expiration and refresh proactively
- Use PKCE for public clients: Mobile and single-page applications should use PKCE
Security Considerations
- Use HTTPS for all OAuth requests
- Validate redirect URIs to prevent authorization code interception
- Implement state parameters to prevent CSRF attacks
- Rotate client secrets periodically
- Monitor token usage for suspicious activity
