OAuth

Access Tokensanchor

availability

OAuth is in closed beta in production, and open beta in sandbox. Contact us to express interest in the production beta release.

Once the merchant has agreed to your requested OAuth scopes, they'll be automatically sent to the redirect URI you specified when generating the connect URL. The final step in the OAuth sequence is for your server to use this redirect URI to create an OAuth access token for the merchant.

Values returned in the redirect URIanchor

The redirect URI will include the following values as query parameters:

Query Parameter Description
code The authorization code. Must be exchanged for an access token to make API calls on the merchant's behalf.
merchantId The Braintree identifier for the merchant's account. Used to construct deep links to the Braintree Control Panel and to help our Support team troubleshoot any issues you might encounter.
state The state value you specified when generating the connect URL , if you specified one.

Creating an access tokenanchor

You must exchange the authorization code in the query string of the redirectUri for an accessToken. The accessToken is used to perform actions on a merchant's behalf. The following example creates an accessToken:

  1. Java
BraintreeGateway gateway = new BraintreeGateway("use_your_client_id", "use_your_client_secret");

OAuthCredentialsRequest request = new OAuthCredentialsRequest()
  .code(codeFromQueryString);

Result<OAuthCredentials> result = gateway.oauth().createTokenFromCode(request);

String accessToken = result.getTarget().getAccessToken();
Calendar expiresAt = result.getTarget().getExpiresAt();
String refreshToken = result.getTarget().getRefreshToken();

Managing access tokensanchor

An OAuth accessToken will expire 24 hours from its creation. To exchange the accessToken (e.g. if the current token is expiring soon or you think it has been compromised in some way), you can use the refreshToken to get a new one. The refreshToken is provided when you get the initial access token and will expire 180 days from its creation. Using a refreshToken will give you both a new accessToken and a new refreshToken.

  1. Java
BraintreeGateway gateway = new BraintreeGateway("use_your_client_id", "use_your_client_secret");

OAuthCredentialsRequest request = new OAuthCredentialsRequest()
  .refreshToken(useTheRefreshToken);

Result<OAuthCredentials> result = gateway.oauth().createTokenFromRefreshToken(request);

String accessToken = result.getTarget().getAccessToken();
Calendar expiresAt = result.getTarget().getExpiresAt();
String refreshToken = result.getTarget().getRefreshToken();

You can then revoke the use of the original access token by providing it to the revoke access token API.

  1. Java
BraintreeGateway gateway = new BraintreeGateway("use_your_client_id", "use_your_client_secret");

Result<OAuthResult> result = gateway.oauth().revokeAccessToken(merchantAccessToken);

The connected merchant can revoke OAuth access via the Control Panel. You can be notified of this event by setting up the OAuth access revoked webhook.

Using a revoked access token will result in an authentication error.

Next stepsanchor