OAuth Flow
Redirect and authorization grant
After completion of the Connect flow, the merchant is sent to the redirect_uri you
provide to connect_url. An authorization code is returned by Braintree in the query
string, along with the state parameter you passed to connect_url.
Here is
an example URL we would redirect to, given a redirect_uri of
https://your.redirect.uri/callback, and a state of foo_state:https://your.redirect.uri/callback?state=foo_state&merchantId=g8cnjbnz83htzgm4&code=8b2cd3963a318b2e
Notice that you also receive back a merchantId. This is a unique identifier for the
account in Braintree's systems, so it can be very useful for support issues; it's also used to
construct deep links to the Braintree Control Panel.
See more details on merchantId in the reference.
Getting an access token
access_token. The
access_token is used to perform actions on a merchant's behalf. The following example
creates an access_token:
- 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();Using an access token
You'll use the access token to perform actions on the merchant's behalf via the Merchant API.
Managing access tokens
access_token will expire 24 hours from its creation. To exchange the
access_token (e.g. if the current token is expiring soon or you think it has been
compromised in some way), you can use the refresh_token to get a new one. The
refresh_token is provided when you get the initial access token and will expire 180
days from its creation. Using a refresh_token will give you both a new
access_token and a new refresh_token.
- 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();- Java
BraintreeGateway gateway = new BraintreeGateway("use_your_client_id", "use_your_client_secret");
Result<oauthresult> result = gateway.oauth().revokeAccessToken(merchantAccessToken);Next Page: →