Skip to content

Commit 6addd6f

Browse files
committed
OAuth work
1 parent 884b441 commit 6addd6f

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

NotionForCmdPalOAuthAPI/Authentication/OAuthClient.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.Net.Http.Headers;
3+
using System.Net.Http.Json;
34
using System.Text;
45
using System.Text.Json;
56
using System.Web;
@@ -70,29 +71,24 @@ public static async Task<IActionResult> HandleOAuthRedirection(Uri response)
7071
var responseMessage = await client.SendAsync(request);
7172
responseMessage.EnsureSuccessStatusCode();
7273

73-
var responseJson = await responseMessage.Content.ReadAsStringAsync();
74-
75-
return new ContentResult()
74+
var responseContent = await responseMessage.Content.ReadFromJsonAsync<OAuthResponse>(new JsonSerializerOptions()
7675
{
77-
StatusCode = 200,
78-
ContentType = "application/json",
79-
Content = responseJson
80-
};
81-
//var responseContent = JsonSerializer.Deserialize<OAuthResponse>(responseJson);
76+
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
77+
});
8278

83-
//if (!responseContent!.IsSuccess)
84-
//{
85-
// return new ContentResult() { StatusCode = 500, ContentType = "text/plain", Content = $"There was a problem authenticating with Notion. Please try again later.\n\n{responseMessage.StatusCode}\n{responseContent.Error}\n{responseContent.ErrorDescription}" };
86-
//}
79+
if (!responseContent!.IsSuccess)
80+
{
81+
return new ContentResult() { StatusCode = 500, ContentType = "text/plain", Content = $"There was a problem authenticating with Notion. Please try again later.\n\n{responseMessage.StatusCode}\n{responseContent.Error}\n{responseContent.ErrorDescription}" };
82+
}
8783

88-
//var responseUrl = $"cmdpalnotionext://oauth_redirect_uri/?access_token={responseContent.AccessToken}&bot_id={responseContent.BotId}";
84+
var responseUrl = $"cmdpalnotionext://oauth_redirect_uri/?access_token={responseContent.AccessToken}&bot_id={responseContent.BotId}";
8985

90-
//return new ContentResult()
91-
//{
92-
// Content = String.Format(successMessage, responseUrl),
93-
// ContentType = "text/html",
94-
// StatusCode = 200
95-
//};
86+
return new ContentResult()
87+
{
88+
Content = String.Format(successMessage, responseUrl),
89+
ContentType = "text/html",
90+
StatusCode = 200
91+
};
9692
}
9793
catch (HttpRequestException ex)
9894
{

0 commit comments

Comments
 (0)