OAuth
Access Tokens
Values returned in the redirect URI
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 token
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:
- C#
BraintreeGateway gateway = new BraintreeGateway(
"use_your_client_id",
"use_your_client_secret"
);
var request = new OAuthCredentialsRequest {
Code = codeFromQueryString
};
Result<OAuthCredentials> result = gateway.OAuth.CreateTokenFromCode(request);
string accessToken = result.Target.AccessToken;
DateTime expiresAt = result.Target.ExpiresAt.Value;
string refreshToken = result.Target.RefreshToken;
Managing access tokens
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.
- C#
BraintreeGateway gateway = new BraintreeGateway(
"use_your_client_id",
"use_your_client_secret"
);
var request = new OAuthCredentialsRequest {
RefreshToken = useTheRefreshToken,
};
Result<oauthcredentials> result = gateway.OAuth.CreateTokenFromRefreshToken(request);
string accessToken = result.Target.AccessToken;
DateTime expiresAt = result.Target.ExpiresAt.Value;
string refreshToken = result.Target.RefreshToken;
- C#
BraintreeGateway gateway = new BraintreeGateway(
"use_your_client_id",
"use_your_client_secret"
);
Result<oauthresult> result = gateway.OAuth.RevokeAccessToken(merchantAccessToken);
Using a revoked access token will result in an authentication error.
Next steps
- See the OAuth Reference
- Learn more about sharing access to payment methods in your own Vault using the Shared Vault and Grant API