From 8479eccad8c5d03b8f039e2fff6706b535a1befc Mon Sep 17 00:00:00 2001 From: Ansel <46641316+JimmyRice@users.noreply.github.com> Date: Thu, 1 Jul 2021 15:38:19 +0800 Subject: [PATCH 1/3] Update authorization docs for C-sharp developers --- book.json | 4 ++ .../oauth/authorization-code-grant.md | 43 +++++++++++++++++++ book/overview/oauth/implicit-grant.md | 20 +++++++++ 3 files changed, 67 insertions(+) diff --git a/book.json b/book.json index 6bbca32..5aadec2 100644 --- a/book.json +++ b/book.json @@ -22,6 +22,10 @@ { "lang": "rust", "name": "Rust" + }, + { + "lang": "cs", + "name": "C#" } ] } diff --git a/book/overview/oauth/authorization-code-grant.md b/book/overview/oauth/authorization-code-grant.md index 77c6242..b3880d1 100644 --- a/book/overview/oauth/authorization-code-grant.md +++ b/book/overview/oauth/authorization-code-grant.md @@ -93,6 +93,49 @@ return json_decode($response->getBody())->access_token; This will return a JSON response containing the JWT `access_token`. {% endtab %} + +{% tab title="C#" %} +```cs +using Flurl; +using Flurl.Http; + +namespace YourApplication +{ + class AuthorizationResult + { + public string token_type { get; set; } + public int expires_in { get; set; } + public string access_token { get; set; } + public string refresh_token { get; set; } + } + + class Program + { + private static string Url = "https://anilist.co/api/v2/oauth/token"; + + public static async Task GetBearerToken() + { + var result = await Url + .PostJsonAsync(new + { + grant_type = "authorization_code", + client_id = "{client_id}", + client_secret = "{client_secret}", + redirect_uri = "{redirect_uri}", + code = "{code}" + }) + .ReceiveJson(); + + return result.access_token; + } + } +} + +``` + +This will return a JSON response containing the JWT `access_token`. +{% endtab %} + {% endtabs %} ## Making Authenticated Requests diff --git a/book/overview/oauth/implicit-grant.md b/book/overview/oauth/implicit-grant.md index e646f1b..2d9d1fb 100644 --- a/book/overview/oauth/implicit-grant.md +++ b/book/overview/oauth/implicit-grant.md @@ -80,5 +80,25 @@ $response = $http->request('POST', 'https://graphql.anilist.co', [ ]); ``` {% endtab %} + +{% tab title="C#" %} +```cs +using Flurl; +using Flurl.Http; + +public static async Task GetUserId() +{ + var result = await "https://graphql.anilist.co" + .WithOAuthBearerToken({YOUR BEARER TOKEN HERE}) + .PostJsonAsync(new + { + query = "{YOUR QUERY STRING HERE}" + }) + .ReceiveString(); // For demo purposes, I will use ReceiveString method. But you should use ReceiveJson method to access the data + + return result; +} +``` +{% endtab %} {% endtabs %} From 064d3c257c5d245d840b38e7a9e04fca6b9e7b59 Mon Sep 17 00:00:00 2001 From: Ansel <46641316+JimmyRice@users.noreply.github.com> Date: Thu, 1 Jul 2021 15:42:57 +0800 Subject: [PATCH 2/3] Rename a method --- book/overview/oauth/implicit-grant.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/overview/oauth/implicit-grant.md b/book/overview/oauth/implicit-grant.md index 2d9d1fb..dc6b0e6 100644 --- a/book/overview/oauth/implicit-grant.md +++ b/book/overview/oauth/implicit-grant.md @@ -86,7 +86,7 @@ $response = $http->request('POST', 'https://graphql.anilist.co', [ using Flurl; using Flurl.Http; -public static async Task GetUserId() +public static async Task GetQueryResult() { var result = await "https://graphql.anilist.co" .WithOAuthBearerToken({YOUR BEARER TOKEN HERE}) From ed97b5321f61328ba83bda57642451c10633af26 Mon Sep 17 00:00:00 2001 From: Ansel <46641316+JimmyRice@users.noreply.github.com> Date: Fri, 2 Jul 2021 14:03:21 +0800 Subject: [PATCH 3/3] Update doc --- .../oauth/authorization-code-grant.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/book/overview/oauth/authorization-code-grant.md b/book/overview/oauth/authorization-code-grant.md index b3880d1..7bd47e3 100644 --- a/book/overview/oauth/authorization-code-grant.md +++ b/book/overview/oauth/authorization-code-grant.md @@ -177,5 +177,26 @@ $response = $http->request('POST', 'https://graphql.anilist.co', [ ]); ``` {% endtab %} + +{% tab title="C#" %} +```cs +using Flurl; +using Flurl.Http; + +public static async Task GetQueryResult() +{ + var result = await "https://graphql.anilist.co" + .WithOAuthBearerToken({YOUR BEARER TOKEN HERE}) + .PostJsonAsync(new + { + query = "{YOUR QUERY STRING HERE}" + }) + .ReceiveString(); // For demo purposes, I will use ReceiveString method. But you should use ReceiveJson method to access the data + + return result; +} +``` +{% endtab %} + {% endtabs %}