Skip to content

Commit

Permalink
Merge pull request #169 from mercadopago/feature/add-oauth-credential…
Browse files Browse the repository at this point in the history
…-method

Add method to create oauth credentials using client id/client secret
  • Loading branch information
romerosilva-meli authored Aug 14, 2023
2 parents 76dc378 + 8bbe39b commit f8a8bea
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/MercadoPago/Client/OAuth/CreateOAuthCredentialRequest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
namespace MercadoPago.Client.OAuth
namespace MercadoPago.Client.OAuth
{
/// <summary>
/// Data to create an OAuth credential.
/// </summary>
public class CreateOAuthCredentialRequest
{
/// <summary>
/// Client secret (Access Token).
/// Client Id
/// </summary>
public string ClientId { get; set; }

/// <summary>
/// Client secret
/// </summary>
public string ClientSecret { get; set; }

Expand Down
78 changes: 74 additions & 4 deletions src/MercadoPago/Client/OAuth/OAuthClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MercadoPago.Client.OAuth
namespace MercadoPago.Client.OAuth
{
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -122,9 +122,9 @@ public string GetAuthorizationURL(
.Append(redirectUri)
.ToString();
}

/// <summary>
/// Creates async an OAuth credentials.
/// Creates an OAuth credentials asynchronously using
/// access token as client secret.
/// </summary>
/// <param name="authorizationCode">Authorization code.</param>
/// <param name="redirectUri">Redirect Uri.</param>
Expand Down Expand Up @@ -165,7 +165,44 @@ public Task<OAuthCredential> CreateOAuthCredentialAsync(
}

/// <summary>
/// Creates an OAuth credentials.
/// Creates an OAuth credentials asynchronously with
/// client id and client secret.
/// </summary>
/// <param name="authorizationCode">Authorization code.</param>
/// <param name="clientId">Client Id.</param>
/// <param name="clientSecret">Client Secret.</param>
/// <param name="redirectUri">Redirect Uri.</param>
/// <param name="requestOptions"><see cref="RequestOptions"/>.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task whose the result is the OAuth credential.</returns>
/// <exception cref="MercadoPagoException">If a unexpected exception occurs.</exception>
/// <exception cref="MercadoPagoApiException">If the API returns a error.</exception>
public Task<OAuthCredential> CreateOAuthCredentialAsync(
string authorizationCode,
string clientId,
string clientSecret,
string redirectUri,
RequestOptions requestOptions = null,
CancellationToken cancellationToken = default)
{
var request = new CreateOAuthCredentialRequest
{
ClientId = clientId,
ClientSecret = clientSecret,
Code = authorizationCode,
RedirectUri = redirectUri,
};
return SendAsync(
"/oauth/token",
HttpMethod.POST,
request,
requestOptions,
cancellationToken);
}

/// <summary>
/// Creates an OAuth credentials using
/// access token as client secret.
/// </summary>
/// <param name="authorizationCode">Authorization code.</param>
/// <param name="redirectUri">Redirect Uri.</param>
Expand Down Expand Up @@ -202,6 +239,39 @@ public OAuthCredential CreateOAuthCredential(
requestOptions);
}

/// <summary>
/// Creates an OAuth credentials with
/// client id and client secret.
/// </summary>
/// <param name="authorizationCode">Authorization code.</param>
/// <param name="clientId">Client Id.</param>
/// <param name="clientSecret">Client Secret.</param>
/// <param name="redirectUri">Redirect Uri.</param>
/// <param name="requestOptions"><see cref="RequestOptions"/>.</param>
/// <returns>The OAuth credential.</returns>
/// <exception cref="MercadoPagoException">If a unexpected exception occurs.</exception>
/// <exception cref="MercadoPagoApiException">If the API returns a error.</exception>
public OAuthCredential CreateOAuthCredential(
string authorizationCode,
string clientId,
string clientSecret,
string redirectUri,
RequestOptions requestOptions = null)
{
var request = new CreateOAuthCredentialRequest
{
ClientId = clientId,
ClientSecret = clientSecret,
Code = authorizationCode,
RedirectUri = redirectUri,
};
return Send(
"/oauth/token",
HttpMethod.POST,
request,
requestOptions);
}

/// <summary>
/// Refresh OAuth credential async.
/// </summary>
Expand Down

0 comments on commit f8a8bea

Please sign in to comment.