Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to create oauth credentials using client id/client secret #169

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esse método não precisa do cancellationToken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somente métodos async possuem cancellationToken.

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
Loading