Skip to content

Add support for create token endpoint #392

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

Merged
merged 1 commit into from
Oct 13, 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
5 changes: 5 additions & 0 deletions Src/Notion.Client/Api/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,10 @@ public static string Create()
return "/v1/comments";
}
}

public static class AuthenticationUrls
{
public static string CreateToken() => "/v1/oauth/token";
}
}
}
12 changes: 12 additions & 0 deletions Src/Notion.Client/Api/Authentication/AuthenticationClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Notion.Client
{
public sealed partial class AuthenticationClient : IAuthenticationClient
{
private readonly IRestClient _client;

public AuthenticationClient(IRestClient restClient)
{
_client = restClient;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading;
using System.Threading.Tasks;

namespace Notion.Client
{
public sealed partial class AuthenticationClient
{
public async Task<CreateTokenResponse> CreateTokenAsync(
CreateTokenRequest createTokenRequest,
CancellationToken cancellationToken = default)
{
var body = (ICreateTokenBodyParameters)createTokenRequest;

return await _client.PostAsync<CreateTokenResponse>(
ApiEndpoints.AuthenticationUrls.CreateToken(),
body,
cancellationToken: cancellationToken
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Notion.Client
{
public class CreateTokenRequest : ICreateTokenBodyParameters
{
public string GrantType => "authorization_code";

public string Code { get; set; }

public string RedirectUri { get; set; }

public ExternalAccount ExternalAccount { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;

namespace Notion.Client
{
/// <summary>
/// External account info
/// </summary>
public class ExternalAccount
{
/// <summary>
/// External account key
/// </summary>
[JsonProperty("key")]
public string Key { get; set; }

/// <summary>
/// External account name
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public interface ICreateTokenBodyParameters
{
/// <summary>
/// A constant string: "authorization_code".
/// </summary>
[JsonProperty("grant_type")]
string GrantType { get; }

/// <summary>
/// A unique random code that Notion generates to authenticate with your service,
/// generated when a user initiates the OAuth flow.
/// </summary>
[JsonProperty("code")]
string Code { get; set; }

/// <summary>
/// The "redirect_uri" that was provided in the OAuth Domain & URI section
/// of the integration's Authorization settings. Do not include this field if a
/// "redirect_uri" query param was not included in the Authorization URL
/// provided to users. In most cases, this field is required.
/// </summary>
[JsonProperty("redirect_uri")]
string RedirectUri { get; set; }

/// <summary>
/// External account details
/// </summary>
[JsonProperty("external_account")]
ExternalAccount ExternalAccount { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;

namespace Notion.Client
{
public class CreateTokenResponse
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }

[JsonProperty("token_type")]
public string TokenType { get; set; } = "bearer";

[JsonProperty("bot_id")]
public string BotId { get; set; }

[JsonProperty("duplicated_template_id")]
public string DuplicatedTemplateId { get; set; }

[JsonProperty("owner")]
public IBotOwner Owner { get; set; }

[JsonProperty("workspace_icon")]
public string WorkspaceIcon { get; set; }

[JsonProperty("workspace_id")]
public string WorkspaceId { get; set; }

[JsonProperty("workspace_name")]
public string WorkspaceName { get; set; }
}
}
22 changes: 22 additions & 0 deletions Src/Notion.Client/Api/Authentication/IAuthenticationClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading;
using System.Threading.Tasks;

namespace Notion.Client
{
/// <summary>
/// Authentication client
/// </summary>
public interface IAuthenticationClient
{
/// <summary>
/// Creates an access token that a third-party service can use to authenticate with Notion.
/// </summary>
/// <param name="createTokenRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<CreateTokenResponse> CreateTokenAsync(
CreateTokenRequest createTokenRequest,
CancellationToken cancellationToken = default
);
}
}
8 changes: 7 additions & 1 deletion Src/Notion.Client/NotionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Notion.Client
[SuppressMessage("ReSharper", "UnusedMemberInSuper.Global")]
public interface INotionClient
{
IAuthenticationClient AuthenticationClient { get; }

IUsersClient Users { get; }

IDatabasesClient Databases { get; }
Expand All @@ -29,7 +31,8 @@ public NotionClient(
IPagesClient pages,
ISearchClient search,
ICommentsClient comments,
IBlocksClient blocks)
IBlocksClient blocks,
IAuthenticationClient authenticationClient)
{
RestClient = restClient;
Users = users;
Expand All @@ -38,8 +41,11 @@ public NotionClient(
Search = search;
Comments = comments;
Blocks = blocks;
AuthenticationClient = authenticationClient;
}

public IAuthenticationClient AuthenticationClient { get; }

public IUsersClient Users { get; }

public IDatabasesClient Databases { get; }
Expand Down
5 changes: 3 additions & 2 deletions Src/Notion.Client/NotionClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public static NotionClient Create(ClientOptions options)
, new DatabasesClient(restClient)
, new PagesClient(restClient)
, new SearchClient(restClient)
, blocks: new BlocksClient(restClient)
, comments: new CommentsClient(restClient)
, new CommentsClient(restClient)
, new BlocksClient(restClient)
, new AuthenticationClient(restClient)
);
}
}
Expand Down