I want to know how to get query parameters in C# language

Asked 1 years ago, Updated 1 years ago, 260 views

Inside the // comment section of the presentation code, I would like to get the code on the program instead of launching the browser by launching Process.Start, but I don't know how to implement it.
I'd like to get the code = part of the URL that appears when I start my browser.

https://mstdn.jp/oauth/authorize/native?code=XXXXXXXXXXXXXXXXXXXX

Get the code by Process.Start() client.PostAsync attempted to retrieve but did not get anything.
*json and query are different things, but I just checked them, so I wrote them down.
I tried to use it again by pasting the code I got once to the variable, but I got an error.

Reference (https://mastodon.example/oauth/authorize portion):https://docs.joinmastodon.org/methods/apps/oauth/

The site I looked up
A https://learn.microsoft.com/ja-jp/dotnet/api/system.net.httpwebrequest?view=net-7.0

Bhttps://arkgame.com/2016/09/04/c%E3%81%A7url%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/

p>
   /*##################################################################################################################
         * the constructor
        ###################################################################################################################*/
        public Client(string client_id,string url)
        {
            instance = url;
            client = new HttpClient();
            client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("test/1.0"));

            Process.Start("explorer.exe", $"\"https://{instance}/oauth/authorize?client_id={client_id}&scope=read&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code\"");
            Console.Write("Input code:");

            auth_code = Console.ReadLine();
            
        }

c#

2022-11-15 01:47

1 Answers

Before that, understand the OAuth 2.0 authorization flow that Mastodon adopted.

cites an illustration of the authorization code flow from .

Enter a description of the image here

Browsers launched in Process.Start() correspond to the flow 2-5 in the figure, and Console.ReadLine() receives the authorization code displayed in the browser corresponding to Figure 6.
By the way, /oauth/token is accessed from 7-8 in the figure.

I want to get that code on the program

If you use a web browser already logged in to the service (in this case, Mustodon), 3-5 will be omitted, so you may want to get the code directly or you may be able to do so, but if you use another browser, you will eventually be asked to log in.

You can receive it by converting it into a web service and specifying the address of the web service in redirect_uri for the program to retrieve it.This step is required as long as you are running on the command line without web service.(Depending on the program's conditions, other steps may work, but I don't know the background, so I can only guide you through the general steps.)


2022-11-15 06:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.