Skip to content

Commit 47be322

Browse files
authored
Merge pull request #392 from notion-dotnet/feature/388-add-authentication-endpoint
Add support for create token endpoint
2 parents b422493 + c932a42 commit 47be322

File tree

10 files changed

+171
-3
lines changed

10 files changed

+171
-3
lines changed

Src/Notion.Client/Api/ApiEndpoints.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,10 @@ public static string Create()
131131
return "/v1/comments";
132132
}
133133
}
134+
135+
public static class AuthenticationUrls
136+
{
137+
public static string CreateToken() => "/v1/oauth/token";
138+
}
134139
}
135140
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Notion.Client
2+
{
3+
public sealed partial class AuthenticationClient : IAuthenticationClient
4+
{
5+
private readonly IRestClient _client;
6+
7+
public AuthenticationClient(IRestClient restClient)
8+
{
9+
_client = restClient;
10+
}
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace Notion.Client
5+
{
6+
public sealed partial class AuthenticationClient
7+
{
8+
public async Task<CreateTokenResponse> CreateTokenAsync(
9+
CreateTokenRequest createTokenRequest,
10+
CancellationToken cancellationToken = default)
11+
{
12+
var body = (ICreateTokenBodyParameters)createTokenRequest;
13+
14+
return await _client.PostAsync<CreateTokenResponse>(
15+
ApiEndpoints.AuthenticationUrls.CreateToken(),
16+
body,
17+
cancellationToken: cancellationToken
18+
);
19+
}
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Notion.Client
2+
{
3+
public class CreateTokenRequest : ICreateTokenBodyParameters
4+
{
5+
public string GrantType => "authorization_code";
6+
7+
public string Code { get; set; }
8+
9+
public string RedirectUri { get; set; }
10+
11+
public ExternalAccount ExternalAccount { get; set; }
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
/// <summary>
6+
/// External account info
7+
/// </summary>
8+
public class ExternalAccount
9+
{
10+
/// <summary>
11+
/// External account key
12+
/// </summary>
13+
[JsonProperty("key")]
14+
public string Key { get; set; }
15+
16+
/// <summary>
17+
/// External account name
18+
/// </summary>
19+
[JsonProperty("name")]
20+
public string Name { get; set; }
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public interface ICreateTokenBodyParameters
6+
{
7+
/// <summary>
8+
/// A constant string: "authorization_code".
9+
/// </summary>
10+
[JsonProperty("grant_type")]
11+
string GrantType { get; }
12+
13+
/// <summary>
14+
/// A unique random code that Notion generates to authenticate with your service,
15+
/// generated when a user initiates the OAuth flow.
16+
/// </summary>
17+
[JsonProperty("code")]
18+
string Code { get; set; }
19+
20+
/// <summary>
21+
/// The "redirect_uri" that was provided in the OAuth Domain & URI section
22+
/// of the integration's Authorization settings. Do not include this field if a
23+
/// "redirect_uri" query param was not included in the Authorization URL
24+
/// provided to users. In most cases, this field is required.
25+
/// </summary>
26+
[JsonProperty("redirect_uri")]
27+
string RedirectUri { get; set; }
28+
29+
/// <summary>
30+
/// External account details
31+
/// </summary>
32+
[JsonProperty("external_account")]
33+
ExternalAccount ExternalAccount { get; set; }
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Notion.Client
4+
{
5+
public class CreateTokenResponse
6+
{
7+
[JsonProperty("access_token")]
8+
public string AccessToken { get; set; }
9+
10+
[JsonProperty("token_type")]
11+
public string TokenType { get; set; } = "bearer";
12+
13+
[JsonProperty("bot_id")]
14+
public string BotId { get; set; }
15+
16+
[JsonProperty("duplicated_template_id")]
17+
public string DuplicatedTemplateId { get; set; }
18+
19+
[JsonProperty("owner")]
20+
public IBotOwner Owner { get; set; }
21+
22+
[JsonProperty("workspace_icon")]
23+
public string WorkspaceIcon { get; set; }
24+
25+
[JsonProperty("workspace_id")]
26+
public string WorkspaceId { get; set; }
27+
28+
[JsonProperty("workspace_name")]
29+
public string WorkspaceName { get; set; }
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace Notion.Client
5+
{
6+
/// <summary>
7+
/// Authentication client
8+
/// </summary>
9+
public interface IAuthenticationClient
10+
{
11+
/// <summary>
12+
/// Creates an access token that a third-party service can use to authenticate with Notion.
13+
/// </summary>
14+
/// <param name="createTokenRequest"></param>
15+
/// <param name="cancellationToken"></param>
16+
/// <returns></returns>
17+
Task<CreateTokenResponse> CreateTokenAsync(
18+
CreateTokenRequest createTokenRequest,
19+
CancellationToken cancellationToken = default
20+
);
21+
}
22+
}

Src/Notion.Client/NotionClient.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Notion.Client
55
[SuppressMessage("ReSharper", "UnusedMemberInSuper.Global")]
66
public interface INotionClient
77
{
8+
IAuthenticationClient AuthenticationClient { get; }
9+
810
IUsersClient Users { get; }
911

1012
IDatabasesClient Databases { get; }
@@ -29,7 +31,8 @@ public NotionClient(
2931
IPagesClient pages,
3032
ISearchClient search,
3133
ICommentsClient comments,
32-
IBlocksClient blocks)
34+
IBlocksClient blocks,
35+
IAuthenticationClient authenticationClient)
3336
{
3437
RestClient = restClient;
3538
Users = users;
@@ -38,8 +41,11 @@ public NotionClient(
3841
Search = search;
3942
Comments = comments;
4043
Blocks = blocks;
44+
AuthenticationClient = authenticationClient;
4145
}
4246

47+
public IAuthenticationClient AuthenticationClient { get; }
48+
4349
public IUsersClient Users { get; }
4450

4551
public IDatabasesClient Databases { get; }

Src/Notion.Client/NotionClientFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public static NotionClient Create(ClientOptions options)
1212
, new DatabasesClient(restClient)
1313
, new PagesClient(restClient)
1414
, new SearchClient(restClient)
15-
, blocks: new BlocksClient(restClient)
16-
, comments: new CommentsClient(restClient)
15+
, new CommentsClient(restClient)
16+
, new BlocksClient(restClient)
17+
, new AuthenticationClient(restClient)
1718
);
1819
}
1920
}

0 commit comments

Comments
 (0)